Skip to content
Related Articles

Related Articles

Program to check if N is a Centered Cubic Number

View Discussion
Improve Article
Save Article
  • Last Updated : 18 Sep, 2022
View Discussion
Improve Article
Save Article

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!