Centered cube number


The problem statement includes printing the N-th centered cube number for some positive value of N, which will be the user input.

A centered cube number is the number of points in a three-dimensional pattern created by a point surrounded by concentric cubical layers of points, with i^2 points on the square faces of the ith layer. It is equivalently the number of points in a body-centered cubic pattern within the cube with n + 1 points along each of its edges.

You can refer to wikipedia for figurative representation of the centered cube number which will help in better understanding of the topic.

The first number of the sequence of centered cube numbers is 1 represented as a point in the centre. Followed by, the second number is 9 represented as a point in the centre surrounded by a cubic layer of points with 1 point on the square face making the number to be 9.

The similar pattern is being followed in the next centered cube numbers representing the total number of points in the pattern. The first few centered cube numbers are 1, 9, 35, 91, 189, 341, 559, 855…….

Each number in the sequence represents the number of points in a three-dimensional pattern of a point surrounded by (N-1) concentric cubical layers of points where N is the position of the centered cube number in the sequence.

In this problem, we will be given a positive integer, say N and our task is to find the value of the centered cube number corresponding to the value of N given.

Example

INPUT : N=4
OUTPUT : 91

Explanation − The value of N given in input is 4. The 4th centered cube number is 91 representing the total number of points in the 3-D pattern where a centred point is surrounded by 3 concentric cubical layers of the points.

INPUT : N=6
OUTPUT : 341

Explanation − The value of centered cube number corresponding to the N i.e. 6 is 341 representing total number of points when a centred point is surrounded by 5 layers of concentric cubes where each i-th cube has i^2 points on the square faces.

Let’s try to understand the algorithm to solve the problem of printing the N-th centered cube number.

Algorithm

There is a mathematical relation between the numbers present in the sequence of the centered cube numbers. Since every number represents the total number of points in the 3-D pattern where a centred point is being surrounded by the concentric layers of the cube formed by points. Every N-th number is surrounded by (N-1) concentric cubic layers of the points.

The mathematical formula to give any N-th centered cube number of the sequence is −

(centered cube number)𝑛 = (𝑛 − 1)3 + 𝑛3

Where, n is the position of the centered cube number in the sequence.

This formula gives the number of points in a 3-D pattern when a point in the centre is surrounded by (n-1) concentric cubical layers formed of points. Every n-th centered cube number can be represented in the form of the above mentioned cubic equation.

Let’s try the equation for n=6.

Since we know that the 6th centered cube number is 341.

Putting n=6 in the equation,

(𝑛 − 1)3 + 𝑛3 = (6 − 1)3 + 63 = 125 + 216 = 341, which is the 6th centered cube number.

Hence, we will be using the formula (𝑛 − 1)3 + 𝑛3 to calculate the N-th centered cube number for any positive value of n in our approach in order to solve the problem.

Approach

The steps to follow to integrate the formula to find the N-th centred cube number in our approach are as follows −

  • To calculate the N-th centered cube number, we will create a function.

  • We will initialise a variable to store the value of N-th centered cube number. The variable will be of long long data type to ensure the larger values of N-th centered cube number for larger values of N.

  • Using the formula (𝑛 − 1)3 + 𝑛3 , we will calculate the centered cube number for the given value of N and store it in the variable.

  • Return the variable, which is the output we require.

Example

The C++ code for the approach −

//C++ code to print the N-th centered cube number for the given value of number
#include <bits/stdc++.h>
using namespace std;

//function to calculate the N-th centered cube number 
long long Nth_number(int N){

   //long long data type is used to store centered cube number for large value of N
   long long a; //to store the number
   a = (N-1)*(N-1)*(N-1) + N*N*N; //using the formula (n-1)^3+n^3
   return a; //return the value stored in a
}
int main(){
   int N;
   N=11;
   //calling the function
   cout<<"The centered cube number for N="<<N<<" is "<<Nth_number(N)<<endl;
   N=28;
   cout<<"The centered cube number for N="<<N<<" is "<<Nth_number(N)<<endl;
   return 0;
}

Output

The centered cube number for N=11 is 2331
The centered cube number for N=28 is 41635

Time Complexity : O(1) , as the formula took constant time to perform the operation.

Space Complexity : O(1) , because we didn’t use any extra space while solving the problem.

Conclusion

The concept of centered cube number was discussed in the article and the mathematical relation among the numbers in the sequence of centered cube numbers to calculate the N-th centered cube number which we used in our approach to solve the problem in C++ within constant time and space.

I hope all your concepts of centered cube numbers are cleared after reading this article.

Updated on: 27-Sep-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements