Posted on by Kalkicode
Code Number

Centered Cube Number

A centered cube number is a number that can be expressed in the form (2n + 1)(n² + n + 1), where n is a non-negative integer. In other words, it is the product of an odd number and a centered square number. The centered square number for a given n is obtained by adding the numbers in a square pattern starting from the center and moving outward. The centered cube number sequence starts with 1 and continues as 9, 35, 91, 189, 341, and so on.

The provided code calculates and prints the first k centered cube numbers. It takes an input value k and iterates from 0 to k-1, calculating the centered cube number for each iteration using the formula mentioned above. The calculated number is then displayed on the screen.

Example:

Let's take k = 5 as an example to understand how the code works.

For n = 0, the formula becomes: (2 * 0 + 1) * (0² + 0 + 1) = 1 * 1 = 1

For n = 1, the formula becomes: (2 * 1 + 1) * (1² + 1 + 1) = 3 * 3 = 9

For n = 2, the formula becomes: (2 * 2 + 1) * (2² + 2 + 1) = 5 * 7 = 35

For n = 3, the formula becomes: (2 * 3 + 1) * (3² + 3 + 1) = 7 * 13 = 91

For n = 4, the formula becomes: (2 * 4 + 1) * (4² + 4 + 1) = 9 * 21 = 189

Thus, the first 5 centered cube numbers are 1, 9, 35, 91, and 189.

Algorithm:

  1. Define a function centeredCubeNo that takes an integer k as input.
  2. Initialize a variable ans to 0.
  3. Iterate from n = 0 to n = k-1:
    • Calculate the centered cube number using the formula: ans = (2 * n + 1) * (n² + n + 1).
    • Print the calculated number.
  4. End the function.
  5. In the main function:
    • Call the centeredCubeNo function with a specific value of k.
    • Return 0 to indicate successful execution.

Pseudocode:


function centeredCubeNo(k):
    ans = 0
    for n = 0 to k-1:
        ans = (2 * n + 1) * (n² + n + 1)
        print ans
main():
centeredCubeNo(10)
return 0

Code Solution

Here given code implementation process.

// C Program for
// Centered Cube Number
#include <stdio.h>

void centeredCubeNo(int k)
{
	int ans = 0;
	// Print all initial k centered cube number
	for (int n = 0; n < k; ++n)
	{
		// Centered cube 
		// formula =  (2n + 1)(n²+n+1)
		// Calculate nth centered number 
		ans = ((2 *n) + 1) *((n *n) + n + 1);
		// Display calculate number
		printf("  %d", ans);
	}
}
int main()
{
	// Centered cube numbers
	// --------------------------------------
	// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
	// Test 
	// k = 10
	centeredCubeNo(10);
	return 0;
}

Output

  1  9  35  91  189  341  559  855  1241  1729
// Java program for
//  Centered Cube Number
public class CubeNumber
{
	public void centeredCubeNo(int k)
	{
		int ans = 0;
		// Print all initial k centered cube number
		for (int n = 0; n < k; ++n)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1);
          
			// Display calculate number
			System.out.print(" " + ans);
		}
	}
	public static void main(String[] args)
	{
		CubeNumber task = new CubeNumber();
		// Centered cube numbers
		// --------------------------------------
		// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
		// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
		// Test 
		// k = 10
		task.centeredCubeNo(10);
	}
}

Output

 1 9 35 91 189 341 559 855 1241 1729
// Include header file
#include <iostream>
using namespace std;
// C++ program for
//  Centered Cube Number
class CubeNumber
{
	public: void centeredCubeNo(int k)
	{
		int ans = 0;
		// Print all initial k centered cube number
		for (int n = 0; n < k; ++n)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			ans = ((2 *n) + 1) *((n *n) + n + 1);
          
			// Display calculate number
			cout << " " << ans;
		}
	}
};
int main()
{
	CubeNumber *task = new CubeNumber();
	// Centered cube numbers
	// --------------------------------------
	// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
  
	// Test 
	// k = 10
	task->centeredCubeNo(10);
	return 0;
}

Output

 1 9 35 91 189 341 559 855 1241 1729
// Include namespace system
using System;
// Csharp program for
//  Centered Cube Number
public class CubeNumber
{
	public void centeredCubeNo(int k)
	{
		int ans = 0;
		// Print all initial k centered cube number
		for (int n = 0; n < k; ++n)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1);
          
			// Display calculate number
			Console.Write(" " + ans);
		}
	}
	public static void Main(String[] args)
	{
		CubeNumber task = new CubeNumber();
		// Centered cube numbers
		// --------------------------------------
		// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
		// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
      
		// Test 
		// k = 10
		task.centeredCubeNo(10);
	}
}

Output

 1 9 35 91 189 341 559 855 1241 1729
package main
import "fmt"
// Go program for
// Centered Cube Number

func centeredCubeNo(k int) {
	var ans int = 0
	// Print all initial k centered cube number
	for n := 0 ; n < k ; n++ {
		// Centered cube 
		// formula =  (2n + 1)(n²+n+1)
      
		// Calculate nth centered number 
		ans = ((2 * n) + 1) * ((n * n) + n + 1)
      
		// Display calculate number
		fmt.Print(" ", ans)
	}
}
func main() {
	// Centered cube numbers
	// --------------------------------------
	// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
  
	// Test 
	// k = 10
	centeredCubeNo(10)
}

Output

 1 9 35 91 189 341 559 855 1241 1729
<?php
// Php program for
//  Centered Cube Number
class CubeNumber
{
	public	function centeredCubeNo($k)
	{
		$ans = 0;
		// Print all initial k centered cube number
		for ($n = 0; $n < $k; ++$n)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			$ans = ((2 * $n) + 1) * (($n * $n) + $n + 1);
          
			// Display calculate number
			echo(" ".$ans);
		}
	}
}

function main()
{
	$task = new CubeNumber();
	// Centered cube numbers
	// --------------------------------------
	// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
  
	// Test 
	// k = 10
	$task->centeredCubeNo(10);
}
main();

Output

 1 9 35 91 189 341 559 855 1241 1729
// Node JS program for
//  Centered Cube Number
class CubeNumber
{
	centeredCubeNo(k)
	{
		var ans = 0;
		// Print all initial k centered cube number
		for (var n = 0; n < k; ++n)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1);
          
			// Display calculate number
			process.stdout.write(" " + ans);
		}
	}
}

function main()
{
	var task = new CubeNumber();
	// Centered cube numbers
	// --------------------------------------
	// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
	// Test 
	// k = 10
	task.centeredCubeNo(10);
}
main();

Output

 1 9 35 91 189 341 559 855 1241 1729
#  Python 3 program for
#   Centered Cube Number
class CubeNumber :
	def centeredCubeNo(self, k) :
		ans = 0
		n = 0
		#  Print all initial k centered cube number
		while (n < k) :
			#  Centered cube 
			#  formula =  (2n + 1)(n²+n+1)
            
			#  Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1)

			#  Display calculate number
			print(" ", ans, end = "")
			n += 1
		
	

def main() :
	task = CubeNumber()
	#  Centered cube numbers
	#  --------------------------------------
	#  1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	#  1729, 2331, 3059, 3925, 4941, 6119 .. etc
    
	#  Test 
	#  k = 10
	task.centeredCubeNo(10)

if __name__ == "__main__": main()

Output

  1  9  35  91  189  341  559  855  1241  1729
#  Ruby program for
#   Centered Cube Number
class CubeNumber 
	def centeredCubeNo(k) 
		ans = 0
		n = 0
		#  Print all initial k centered cube number
		while (n < k) 
			#  Centered cube 
			#  formula =  (2n + 1)(n²+n+1)
            
			#  Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1)

			#  Display calculate number
			print(" ", ans)
			n += 1
		end

	end

end

def main() 
	task = CubeNumber.new()
	#  Centered cube numbers
	#  --------------------------------------
	#  1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	#  1729, 2331, 3059, 3925, 4941, 6119 .. etc
    
	#  Test 
	#  k = 10
	task.centeredCubeNo(10)
end

main()

Output

 1 9 35 91 189 341 559 855 1241 1729
// Scala program for
//  Centered Cube Number
class CubeNumber()
{
	def centeredCubeNo(k: Int): Unit = {
		var ans: Int = 0;
		var n: Int = 0;
		// Print all initial k centered cube number
		while (n < k)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1);
          
			// Display calculate number
			print(" " + ans);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CubeNumber = new CubeNumber();
		// Centered cube numbers
		// --------------------------------------
		// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
		// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
  
		// Test 
		// k = 10
		task.centeredCubeNo(10);
	}
}

Output

 1 9 35 91 189 341 559 855 1241 1729
// Swift 4 program for
//  Centered Cube Number
class CubeNumber
{
	func centeredCubeNo(_ k: Int)
	{
		var ans: Int = 0;
		var n: Int = 0;
		// Print all initial k centered cube number
		while (n < k)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1);
          
			// Display calculate number
			print(" ", ans, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: CubeNumber = CubeNumber();
	// Centered cube numbers
	// --------------------------------------
	// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
  
	// Test 
	// k = 10
	task.centeredCubeNo(10);
}
main();

Output

  1  9  35  91  189  341  559  855  1241  1729
// Kotlin program for
//  Centered Cube Number
class CubeNumber
{
	fun centeredCubeNo(k: Int): Unit
	{
		var ans: Int ;
		var n: Int = 0;
		// Print all initial k centered cube number
		while (n < k)
		{
			// Centered cube 
			// formula =  (2n + 1)(n²+n+1)
          
			// Calculate nth centered number 
			ans = ((2 * n) + 1) * ((n * n) + n + 1);
          
			// Display calculate number
			print(" " + ans);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CubeNumber = CubeNumber();
	// Centered cube numbers
	// --------------------------------------
	// 1, 9, 35, 91, 189, 341, 559, 855, 1241, 
	// 1729, 2331, 3059, 3925, 4941, 6119 .. etc
  
	// Test 
	// k = 10
	task.centeredCubeNo(10);
}

Output

 1 9 35 91 189 341 559 855 1241 1729

Output Explanation:

The code is executed with k = 10. It calculates and prints the first 10 centered cube numbers. The output is as follows:

1 9 35 91 189 341 559 855 1241 1729

These are the first 10 numbers in the centered cube number sequence.

Time Complexity:

The time complexity of the code is O(k) because it iterates k times, and the calculations inside the loop have constant time complexity. The formula used to calculate the centered cube number can be computed in constant time for each iteration.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment