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 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.
- The Nth term of a centered cubic number is given by .
- Run a loop starting from 1, to find ith centered cube number.
- Check if the i-th term is equal to N or not. If it is equal, then return true.
- If i-th term is greater than N, then return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isCenteredcube( int N)
{
int i = 1;
while ( true ) {
int ith_term = (2 * i + 1)
* (i * i + i + 1);
if (ith_term == N) {
return true ;
}
if (ith_term > N) {
return false ;
}
i++;
}
}
int main()
{
int N = 9;
if (isCenteredcube(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static boolean isCenteredcube( int N)
{
int i = 1 ;
while ( true )
{
int ith_term = ( 2 * i + 1 ) *
(i * i + i + 1 );
if (ith_term == N)
{
return true ;
}
if (ith_term > N)
{
return false ;
}
i++;
}
}
public static void main(String[] args)
{
int N = 9 ;
if (isCenteredcube(N))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def isCenteredcube(N):
i = 1 ;
while ( True ):
ith_term = (( 2 * i + 1 ) *
(i * i + i + 1 ));
if (ith_term = = N):
return True ;
if (ith_term > N):
return False ;
i + = 1 ;
N = 9 ;
if (isCenteredcube(N)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static Boolean isCenteredcube( int N)
{
int i = 1;
while ( true )
{
int ith_term = (2 * i + 1) *
(i * i + i + 1);
if (ith_term == N)
{
return true ;
}
if (ith_term > N)
{
return false ;
}
i++;
}
}
public static void Main()
{
int N = 9;
if (isCenteredcube(N))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function isCenteredcube(N)
{
let i = 1;
while ( true ) {
let ith_term = (2 * i + 1) * (i * i + i + 1);
if (ith_term == N) {
return true ;
}
if (ith_term > N) {
return false ;
}
i++;
}
}
let N = 9;
if (isCenteredcube(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
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>
bool isCenteredCubic( int n)
{
int k = static_cast < int >(cbrt(n / 2));
return n == pow (2 * k + 1, 3) - pow (2 * k, 3);
}
int main()
{
int n = 33;
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 {
static bool IsCenteredCubic( int n)
{
int k = ( int )Math.Cbrt(n / 2.0);
return n
== Math.Pow(2 * k + 1, 3) - Math.Pow(2 * k, 3);
}
static void Main()
{
int n = 33;
bool result = IsCenteredCubic(n);
Console.WriteLine(result ? "true" : "false" );
}
}
|
Javascript
function isCenteredCubic(n) {
const k = Math.floor(Math.cbrt(n / 2));
return n === Math.pow(2 * k + 1, 3) - Math.pow(2 * k, 3);
}
console.log(isCenteredCubic(33));
|
time complexity :O(1)
space complexity:O(1)
Last Updated :
27 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...