Centered cube number

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to find nth Centered cube
// number
#include <bits/stdc++.h>
using namespace std;
  
// Function to find
// Centered cube number
int centered_cube(int n)
{
    // Formula to calculate nth
    // Centered cube number &
    // return it into main function.
    return (2 * n + 1) * ( n * n + n + 1);
}
  
// Driver Code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find nth Centered
// cube number
import java.io.*;
  
class GFG {
      
    // Function to find
    // Centered cube number
    static int centered_cube(int n)
    {
        // Formula to calculate nth
        // Centered cube number &
        // return it into main function.
        return (2 * n + 1) * ( n * n + n + 1);
    }
      
    // Driver code
    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));
    }
}
  
// This code is contributed by m_kit.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find 
# nth Centered cube number
  
# Centered cube 
# number function
def centered_cube(n) :
      
    # Formula to calculate 
    # nth Centered cube 
    # number return it 
    # into main function.
    return (2 * n + 1) * (
                n * n + n + 1)
  
# Driver Code
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))
  
# This code is contributed by ajit

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find nth
// Centered cube number
using System;
  
class GFG
{
      
    // Function to find
    // Centered cube number
    static int centered_cube(int n)
    {
        // Formula to calculate 
        // nth Centered cube 
        // number & return it
        // into main function.
        return (2 * n + 1) * 
               (n * n + n + 1);
    }
      
    // Driver code
    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));
    }
}
  
// This code is contributed by aj_36

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to find nth 
// Centered cube number
  
// Function to find
// Centered cube number
function centered_cube($n)
{
    // Formula to calculate nth
    // Centered cube number &
    // return it into main function.
    return (2 * $n + 1) * 
           ($n * $n + $n + 1);
}
  
// Driver Code
$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);
  
// This code is contributed by m_kit
?>

chevron_right



Output :

 3th Centered cube number: 91
 10th Centered cube number: 2331


My Personal Notes arrow_drop_up