Program to check if N is a Centered Cubic Number

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

A centred 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes

Time Complexity: O(N).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.