Open In App

Program to check if N is a Centered Cubic Number

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to check if N is a centered cubic number or not.
 

A centered cubic number counts the number of points which are formed by a point that is surrounded by concentric cubical layers in 3D with i2 points on the square faces of the i-th layer. The first few Centered cube numbers are 1, 9, 35, 91, 189, 341, 559, 855 … 
 


Examples: 

Input: N = 9 
Output: Yes 
Explanation: 
9 is the second Centered cube number
Input: N = 6 
Output: No 
 


Approach: The idea is to iterate from one and check whether the ith term is equal to N or not. 
 

  1. The Nth term of a centered cubic number is given by (2 * N + 1) * ( N^2 + N + 1)            .
  2. Run a loop starting from 1, to find ith centered cube number.
  3. Check if the i-th term is equal to N or not. If it is equal, then return true.
  4. If i-th term is greater than N, then return false.


Below is the implementation of the above approach:
 

C++

// C++ program to check if N
// is a centered cubic number
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the number N
// is a centered cubic number
bool isCenteredcube(int N)
{
// Iterating from 1
    int i = 1;
 
// Infinite loop
    while (true) {
 
        // Finding ith_term
        int ith_term = (2 * i + 1)
* (i * i + i + 1);
 
        // Checking if the number N
        // is a Centered cube number
        if (ith_term == N) {
            return true;
        }
 
        // If ith_term > N then
        // N is not a Centered cube number
        if (ith_term > N) {
            return false;
        }
 
        // Incrementing i
        i++;
    }
}
 
// Driver code
int main()
{
    int N = 9;
 
    // Function call
    if (isCenteredcube(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

                    

Java

// Java program to check if N
// is a centered cubic number
class GFG{
 
// Function to check if N
// is a centered cubic number
static boolean isCenteredcube(int N)
{
     
    // Iterating from 1
    int i = 1;
     
    // Infinite loop
    while (true)
    {
         
        // Finding ith_term
        int ith_term = (2 * i + 1) *
                       (i * i + i + 1);
 
        // Checking if the number N
        // is a centered cube number
        if (ith_term == N)
        {
            return true;
        }
 
        // If ith_term > N then N is 
        // not a centered cube number
        if (ith_term > N)
        {
            return false;
        }
 
        // Incrementing i
        i++;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 9;
 
    // Function call
    if (isCenteredcube(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by shubham

                    

Python3

# Python3 program to check if N
# is a centered cubic number
 
# Function to check if N
# is a centered cubic number
def isCenteredcube(N):
 
    # Iterating from 1
    i = 1;
     
    # Infinite loop
    while (True):
     
        # Finding ith_term
        ith_term = ((2 * i + 1) *
                    (i * i + i + 1));
 
        # Checking if the number N
        # is a centered cube number
        if (ith_term == N):
            return True;
         
        # If ith_term > N then N is
        # not a centered cube number
        if (ith_term > N):
            return False;
         
        # Incrementing i
        i += 1;
     
# Driver code
N = 9;
 
# Function call
if (isCenteredcube(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Code_Mech

                    

C#

// C# program to check if N
// is a centered cubic number
using System;
class GFG{
 
// Function to check if N
// is a centered cubic number
static Boolean isCenteredcube(int N)
{
     
    // Iterating from 1
    int i = 1;
     
    // Infinite loop
    while (true)
    {
         
        // Finding ith_term
        int ith_term = (2 * i + 1) *
                       (i * i + i + 1);
 
        // Checking if the number N
        // is a centered cube number
        if (ith_term == N)
        {
            return true;
        }
 
        // If ith_term > N then N is
        // not a centered cube number
        if (ith_term > N)
        {
            return false;
        }
 
        // Incrementing i
        i++;
    }
}
 
// Driver code
public static void Main()
{
    int N = 9;
     
    // Function call
    if (isCenteredcube(N))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by shivanisinghss2110

                    

Javascript

<script>
    // Javascript program to check if N 
    // is a centered cubic number
     
    // Function to check if the number N
    // is a centered cubic number
    function isCenteredcube(N)
    {
        // Iterating from 1
        let i = 1;
 
        // Infinite loop
        while (true) {
 
            // Finding ith_term
            let ith_term = (2 * i + 1) * (i * i + i + 1);
 
            // Checking if the number N
            // is a Centered cube number
            if (ith_term == N) {
                return true;
            }
 
            // If ith_term > N then
            // N is not a Centered cube number
            if (ith_term > N) {
                return false;
            }
 
            // Incrementing i
            i++;
        }
    }
     
    let N = 9;
   
    // Function call
    if (isCenteredcube(N)) {
        document.write("Yes");
    }
    else {
        document.write("No");
    }
 
    // This code is contributed by divyesh072019.
</script>

                    

Output
Yes





Time Complexity: O(N).

Auxiliary Space: O(1) as constant space for variables is being used

Example-2:

Approach:

This implementation takes a single argument n and returns True if n is a centered cubic number, and False otherwise.

The function first calculates the largest integer k such that k^3 is less than or equal to n / 2. We then check if n is equal to the difference between the cubes of 2k + 1 and 2k. If n matches this condition, then it is a centered cubic number.

C++

#include <cmath>
#include <iostream>
 
// Function to check if a number is a centered cubic number
bool isCenteredCubic(int n)
{
    // Calculate k, the integer cube root of n divided by 2
    int k = static_cast<int>(cbrt(n / 2));
 
    // Check if n is equal to (2k + 1)^3 - (2k)^3
    return n == pow(2 * k + 1, 3) - pow(2 * k, 3);
}
 
int main()
{
    // Test with a specific number
    int n = 33;
 
    // Print the result of the isCenteredCubic function
    bool result = isCenteredCubic(n);
    std::cout << (result ? "true" : "false") << std::endl;
 
    return 0;
}

                    

Java

public class GFG {
    public static boolean isCenteredCubic(int n)
    {
        int k = (int)Math.cbrt(n / 2);
        return n
            == Math.pow(2 * k + 1, 3) - Math.pow(2 * k, 3);
    }
 
    public static void main(String[] args)
    {
        int n = 33;
        System.out.println(isCenteredCubic(n));
    }
}

                    

Python3

def is_centered_cubic(n):
    k = int((n / 2) ** (1 / 3))
    return n == (2 * k + 1) ** 3 - (2 * k) ** 3
 
print(is_centered_cubic(33))

                    

C#

using System;
 
class Program {
    // Function to check if a number is a centered cubic
    // number
    static bool IsCenteredCubic(int n)
    {
        // Calculate k, the integer cube root of n divided
        // by 2
        int k = (int)Math.Cbrt(n / 2.0);
 
        // Check if n is equal to (2k + 1)^3 - (2k)^3
        return n
            == Math.Pow(2 * k + 1, 3) - Math.Pow(2 * k, 3);
    }
 
    static void Main()
    {
        // Test with a specific number
        int n = 33;
 
        // Print the result of the IsCenteredCubic function
        bool result = IsCenteredCubic(n);
        Console.WriteLine(result ? "true" : "false");
    }
}

                    

Javascript

function isCenteredCubic(n) {
    // Calculate the integer k such that k^3 is closest to n/2
    const k = Math.floor(Math.cbrt(n / 2));
 
    // Check if n is equal to the difference between (2k+1)^3 and (2k)^3
    return n === Math.pow(2 * k + 1, 3) - Math.pow(2 * k, 3);
}
 
console.log(isCenteredCubic(33));

                    

Output
False






 time complexity :O(1)

space complexity:O(1)



Last Updated : 27 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads