Given a number n, find the n-th centered cube number.
The Centered cube 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. Source[WIKI]. Please see this image for more clarity.
The first few Centered cube numbers are:
1, 9, 35, 91, 189, 341, 559, 855, 1241, 172…………………………
Examples :
Input : n = 1
Output : 9
Input : n = 7
Output : 855
Mathematical formula for nth centered cube number is given by:
n-th Centered Cube Number = (2n + 1)(n2 + n + 1)
Below is the basic implementation of the above formula:
C++
#include <bits/stdc++.h>
using namespace std;
int centered_cube( int n)
{
return (2 * n + 1) * ( n * n + n + 1);
}
int main()
{
int n = 3;
cout << n << "th Centered cube number: " ;
cout << centered_cube(n);
cout << endl;
n = 10;
cout << n << "th Centered cube number: " ;
cout << centered_cube(n);
return 0;
}
|
C
#include <stdio.h>
int centered_cube( int n)
{
return (2 * n + 1) * ( n * n + n + 1);
}
int main()
{
int n = 3;
printf ( "%dth Centered cube number: " ,n);
printf ( "%d\n" ,centered_cube(n));
n = 10;
printf ( "%dth Centered cube number: " ,n);
printf ( "%d\n" ,centered_cube(n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int centered_cube( int n)
{
return ( 2 * n + 1 ) * ( n * n + n + 1 );
}
public static void main (String[] args)
{
int n = 3 ;
System.out.print (n + "th Centered"
+ " cube number: " );
System.out.println (centered_cube(n));
n = 10 ;
System.out.print ( n + "th Centered"
+ " cube number: " );
System.out.println (centered_cube(n));
}
}
|
Python3
def centered_cube(n) :
return ( 2 * n + 1 ) * (
n * n + n + 1 )
if __name__ = = '__main__' :
n = 3
print (n, "th Centered cube " +
"number : " ,
centered_cube(n))
n = 10
print (n, "th Centered cube " +
"number : " ,
centered_cube(n))
|
C#
using System;
class GFG
{
static int centered_cube( int n)
{
return (2 * n + 1) *
(n * n + n + 1);
}
static public void Main ()
{
int n = 3;
Console.Write(n + "th Centered" +
" cube number: " );
Console.WriteLine (centered_cube(n));
n = 10;
Console.Write( n + "th Centered" +
" cube number: " );
Console.WriteLine(centered_cube(n));
}
}
|
PHP
<?php
function centered_cube( $n )
{
return (2 * $n + 1) *
( $n * $n + $n + 1);
}
$n = 3;
echo $n , "th Centered cube number: " ;
echo centered_cube( $n );
echo "\n" ;
$n = 10;
echo $n , "th Centered cube number: " ;
echo centered_cube( $n );
?>
|
Javascript
<script>
function centered_cube(n)
{
return (2 * n + 1) * ( n * n + n + 1);
}
let n = 3;
document.write(n + "th Centered cube number: " );
document.write(centered_cube(n));
document.write( "<br>" );
n = 10;
document.write(n + "th Centered cube number: " );
document.write(centered_cube(n));
</script>
|
Output :
3th Centered cube number: 91
10th Centered cube number: 2331
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!