Posted on by Kalkicode
Code Number

Centered octahedral number

Centered octahedral numbers are a special sequence of numbers derived from a geometric shape called an octahedron. An octahedron is a polyhedron with eight faces, and centered octahedral numbers are the number of balls needed to construct a centered octahedron with layers of increasing sizes. This sequence has numerous applications in various mathematical and geometrical contexts.

Problem Statement

The task is to generate the first 'k' centered octahedral numbers and display them. The formula to calculate the nth centered octahedral number is:

Centered Octahedral Number (nth) = ((2n + 1) * (2n^2 + 2n + 3)) / 3

The goal is to create a program that computes and prints the initial 'k' centered octahedral numbers.

Example: Let's illustrate how the centered octahedral numbers are calculated for 'k = 10':

  • For n = 0, the formula gives us: ((2 * 0 + 1) * (2 * 0^2 + 2 * 0 + 3)) / 3 = 1
  • For n = 1, the formula gives us: ((2 * 1 + 1) * (2 * 1^2 + 2 * 1 + 3)) / 3 = 7
  • For n = 2, the formula gives us: ((2 * 2 + 1) * (2 * 2^2 + 2 * 2 + 3)) / 3 = 25
  • ...and so on until we have 'k' centered octahedral numbers.

Standard Pseudocode

function centeredOctahedralNo(k):
    for n from 0 to k-1:
        result = (((2 * n) + 1) * (2 * n^2 + 2 * n + 3)) / 3
        print result

Algorithm Explanation

  1. Start the function centeredOctahedralNo(k), which takes an integer 'k' as input.
  2. Use a loop to iterate over 'n' from 0 to k-1.
  3. For each value of 'n', calculate the centered octahedral number using the given formula.
  4. Print the calculated centered octahedral number.

Code Solution

Here given code implementation process.

// C Program for
// Centered octahedral number
#include <stdio.h>

void centeredOctahedralNo(int k)
{
	// Print all initial k centered octahedral number
	for (int n = 0; n < k; ++n)
	{
		//   Formula
		//   (2n+1) (2n² + 2n + 3) 
		//   ——————————————————————
		//            3
      
		// Calculate nth octahedral number
		int result = (((2 * n) + 1) *(2 * (n * n) + (2 * n) + 3)) / 3;
      
		// Display Calculated value
		printf("  %d", result);
	}
}
int main()
{
	// Centered octahedral numbers are
	// —————————————————————————————————————————————
	// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
	// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	// 17343, 19649, 22151, 24857, 27775, 30913, 
	// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
  
	//  Test  k = 10
	centeredOctahedralNo(10);
	return 0;
}

Output

  1  7  25  63  129  231  377  575  833  1159
// Java program for
// Centered octahedral number
public class CenteredOctahedral
{
	public void centeredOctahedralNo(int k)
	{
		// Print all initial k centered octahedral number
		for (int n = 0; n < k; ++n)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
          
			// Calculate nth octahedral number
			int result = (((2 * n) + 1) * (2 * (n * n) + (2 * n) + 3)) / 3;
          
			// Display Calculated value
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		CenteredOctahedral task = new CenteredOctahedral();
		// Centered octahedral numbers are
		// —————————————————————————————————————————————
		// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
		// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
		// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
		// 17343, 19649, 22151, 24857, 27775, 30913, 
		// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
		//  Test  k = 10
		task.centeredOctahedralNo(10);
	}
}

Output

 1 7 25 63 129 231 377 575 833 1159
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered octahedral number

class CenteredOctahedral
{
	public: void centeredOctahedralNo(int k)
	{
		// Print all initial k centered octahedral number
		for (int n = 0; n < k; ++n)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
          
			// Calculate nth octahedral number
			int result = (((2 *n) + 1) *(2 *(n *n) + (2 *n) + 3)) / 3;
          
			// Display Calculated value
			cout << " " << result;
		}
	}
};
int main()
{
	CenteredOctahedral *task = new CenteredOctahedral();
	// Centered octahedral numbers are
	// —————————————————————————————————————————————
	// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
	// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	// 17343, 19649, 22151, 24857, 27775, 30913, 
	// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
  
	//  Test  k = 10
	task->centeredOctahedralNo(10);
	return 0;
}

Output

 1 7 25 63 129 231 377 575 833 1159
// Include namespace system
using System;
// Csharp program for
// Centered octahedral number
public class CenteredOctahedral
{
	public void centeredOctahedralNo(int k)
	{
		// Print all initial k centered octahedral number
		for (int n = 0; n < k; ++n)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
          
			// Calculate nth octahedral number
			int result = (((2 * n) + 1) * 
                          (2 * (n * n) + 
                           (2 * n) + 3)) / 3;
          
			// Display Calculated value
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		CenteredOctahedral task = new CenteredOctahedral();
		// Centered octahedral numbers are
		// —————————————————————————————————————————————
		// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
		// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
		// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
		// 17343, 19649, 22151, 24857, 27775, 30913, 
		// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
      
		//  Test  k = 10
		task.centeredOctahedralNo(10);
	}
}

Output

 1 7 25 63 129 231 377 575 833 1159
package main
import "fmt"
// Go program for
// Centered octahedral number

func centeredOctahedralNo(k int) {
	// Print all initial k centered octahedral number
	for n := 0 ; n < k ; n++ {
		//   Formula
		//   (2n+1) (2n² + 2n + 3) 
		//   ——————————————————————
		//            3

		// Calculate nth octahedral number
		var result int = (((2 * n) + 1) * 
			(2 * (n * n) + 
				(2 * n) + 3)) / 3
		
		// Display Calculated value
		fmt.Print(" ", result)
	}
}
func main() {

	// Centered octahedral numbers are
	// —————————————————————————————————————————————
	// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
	// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	// 17343, 19649, 22151, 24857, 27775, 30913, 
	// 34279, 37881, 41727, 45825, 50183, 54809 ... etc

	//  Test  k = 10
	centeredOctahedralNo(10)
}

Output

 1 7 25 63 129 231 377 575 833 1159
<?php
// Php program for
// Centered octahedral number
class CenteredOctahedral
{
	public	function centeredOctahedralNo($k)
	{
		// Print all initial k centered octahedral number
		for ($n = 0; $n < $k; ++$n)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
          
			// Calculate nth octahedral number
			$result = (int)((((2 * $n) + 1) * 
                             (2 * ($n * $n) + 
                              (2 * $n) + 3)) / 3);
			// Display Calculated value
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new CenteredOctahedral();
	// Centered octahedral numbers are
	// —————————————————————————————————————————————
	// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
	// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	// 17343, 19649, 22151, 24857, 27775, 30913, 
	// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
  
	//  Test  k = 10
	$task->centeredOctahedralNo(10);
}
main();

Output

 1 7 25 63 129 231 377 575 833 1159
// Node JS program for
// Centered octahedral number
class CenteredOctahedral
{
	centeredOctahedralNo(k)
	{
		// Print all initial k centered octahedral number
		for (var n = 0; n < k; ++n)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
			// Calculate nth octahedral number
			var result = parseInt((((2 * n) + 1) * 
                                   (2 * (n * n) + 
                                    (2 * n) + 3)) / 3);
			// Display Calculated value
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new CenteredOctahedral();
	// Centered octahedral numbers are
	// —————————————————————————————————————————————
	// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
	// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	// 17343, 19649, 22151, 24857, 27775, 30913, 
	// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
  
	//  Test  k = 10
	task.centeredOctahedralNo(10);
}
main();

Output

 1 7 25 63 129 231 377 575 833 1159
#  Python 3 program for
#  Centered octahedral number
class CenteredOctahedral :
	def centeredOctahedralNo(self, k) :
		n = 0
		#  Print all initial k centered octahedral number
		while (n < k) :
			#    Formula
			#    (2n+1) (2n² + 2n + 3) 
			#    ——————————————————————
			#             3
            
			#  Calculate nth octahedral number
			result = int((((2 * n) + 1) * 
                          (2 * (n * n) + 
                          (2 * n) + 3)) / 3)
			#  Display Calculated value
			print(" ", result, end = "")
			n += 1
		
	

def main() :
	task = CenteredOctahedral()
	#  Centered octahedral numbers are
	#  —————————————————————————————————————————————
	#  1, 7, 25, 63, 129, 231, 377, 575, 833, 
	#  1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	#  6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	#  17343, 19649, 22151, 24857, 27775, 30913, 
	#  34279, 37881, 41727, 45825, 50183, 54809 ... etc
	#   Test  k = 10
	task.centeredOctahedralNo(10)

if __name__ == "__main__": main()

Output

  1  7  25  63  129  231  377  575  833  1159
#  Ruby program for
#  Centered octahedral number
class CenteredOctahedral 
	def centeredOctahedralNo(k) 
		n = 0
		#  Print all initial k centered octahedral number
		while (n < k) 
			#    Formula
			#    (2n+1) (2n² + 2n + 3) 
			#    ——————————————————————
			#             3
            
			#  Calculate nth octahedral number
			result = (((2 * n) + 1) * 
                      (2 * (n * n) + (2 * n) + 3)) / 3

			#  Display Calculated value
			print(" ", result)
			n += 1
		end

	end

end

def main() 
	task = CenteredOctahedral.new()
	#  Centered octahedral numbers are
	#  —————————————————————————————————————————————
	#  1, 7, 25, 63, 129, 231, 377, 575, 833, 
	#  1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	#  6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	#  17343, 19649, 22151, 24857, 27775, 30913, 
	#  34279, 37881, 41727, 45825, 50183, 54809 ... etc
    
	#   Test  k = 10
	task.centeredOctahedralNo(10)
end

main()

Output

 1 7 25 63 129 231 377 575 833 1159
// Scala program for
// Centered octahedral number
class CenteredOctahedral()
{
	def centeredOctahedralNo(k: Int): Unit = {
		var n: Int = 0;
		// Print all initial k centered octahedral number
		while (n < k)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
          
			// Calculate nth octahedral number
			var result: Int = (((2 * n) + 1) * 
                               (2 * (n * n) + 
                                (2 * n) + 3)) / 3;
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CenteredOctahedral = new CenteredOctahedral();
		// Centered octahedral numbers are
		// —————————————————————————————————————————————
		// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
		// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
		// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
		// 17343, 19649, 22151, 24857, 27775, 30913, 
		// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
  
		//  Test  k = 10
		task.centeredOctahedralNo(10);
	}
}

Output

 1 7 25 63 129 231 377 575 833 1159
// Swift 4 program for
// Centered octahedral number
class CenteredOctahedral
{
	func centeredOctahedralNo(_ k: Int)
	{
		var n: Int = 0;
		// Print all initial k centered octahedral number
		while (n < k)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
          
			// Calculate nth octahedral number
			let result: Int = (((2 * n) + 1) * 
                               (2 * (n * n) + 
                                (2 * n) + 3)) / 3;
          
			// Display Calculated value
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: CenteredOctahedral = CenteredOctahedral();
	// Centered octahedral numbers are
	// —————————————————————————————————————————————
	// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
	// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	// 17343, 19649, 22151, 24857, 27775, 30913, 
	// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
  
	//  Test  k = 10
	task.centeredOctahedralNo(10);
}
main();

Output

  1  7  25  63  129  231  377  575  833  1159
// Kotlin program for
// Centered octahedral number
class CenteredOctahedral
{
	fun centeredOctahedralNo(k: Int): Unit
	{
		var n: Int = 0;
		// Print all initial k centered octahedral number
		while (n < k)
		{
			//   Formula
			//   (2n+1) (2n² + 2n + 3) 
			//   ——————————————————————
			//            3
          
			// Calculate nth octahedral number
			val result: Int = (((2 * n) + 1) * 
                               (2 * (n * n) + 
                                (2 * n) + 3)) / 3;
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CenteredOctahedral = CenteredOctahedral();
	// Centered octahedral numbers are
	// —————————————————————————————————————————————
	// 1, 7, 25, 63, 129, 231, 377, 575, 833, 
	// 1159, 1561, 2047, 2625, 3303, 4089, 4991, 
	// 6017, 7175, 8473, 9919, 11521, 13287, 15225, 
	// 17343, 19649, 22151, 24857, 27775, 30913, 
	// 34279, 37881, 41727, 45825, 50183, 54809 ... etc
  
	//  Test  k = 10
	task.centeredOctahedralNo(10);
}

Output

 1 7 25 63 129 231 377 575 833 1159

Output Explanation

Running the program with 'k = 10' generates the following output:

1 7 25 63 129 231 377 575 833 1159

The output shows the first 10 centered octahedral numbers as per the formula.

Time Complexity

The time complexity of the code is O(k), where 'k' is the number of centered octahedral numbers to be generated. This is because the loop iterates 'k' times, and the calculations inside the loop have constant time complexity. Therefore, the overall time complexity is linear with respect to 'k'.

Finally

The article discussed the concept of centered octahedral numbers and provided a C program to generate the first 'k' centered octahedral numbers using the given formula. It explained the algorithm, provided a suitable example, and demonstrated the output for a specific value of 'k'. Additionally, it outlined the time complexity of the code to analyze its efficiency. Centered octahedral numbers find applications in various mathematical and geometrical domains and are of interest to researchers and enthusiasts alike.

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