Posted on by Kalkicode
Code Number

Octahedral number

An octahedral number is a figurate number that represents the number of spheres in an octahedron made by stacking layers of spheres. It can also be visualized as the sum of the first n triangular numbers. The formula for finding the nth octahedral number is n(2n² + 1) / 3.

Problem Statement

The task is to generate the first k octahedral numbers using the given formula and display the results.

Example

Let's take k = 5 as an example to understand the process of generating octahedral numbers.

  1. For n = 1: Octahedral number = (1 * (2 * (1 * 1) + 1)) / 3 = 1
  2. For n = 2: Octahedral number = (2 * (2 * (2 * 2) + 1)) / 3 = 6
  3. For n = 3: Octahedral number = (3 * (2 * (3 * 3) + 1)) / 3 = 19
  4. For n = 4: Octahedral number = (4 * (2 * (4 * 4) + 1)) / 3 = 44
  5. For n = 5: Octahedral number = (5 * (2 * (5 * 5) + 1)) / 3 = 85

Pseudocode

  1. Start with a function to calculate and display the first k octahedral numbers.
  2. Inside the function, use a loop to iterate from n = 1 to n = k.
  3. For each value of n, apply the octahedral number formula (n(2n² + 1) / 3) to find the result.
  4. Print the calculated result for each value of n.

Algorithm

FUNCTION octahedralNo(k):
    FOR n = 1 to k:
        result = (n * (2 * (n * n) + 1)) / 3
        PRINT result
    END FOR
END FUNCTION

FUNCTION main():
    SET k = 10
    CALL octahedralNo(k)
END FUNCTION

Code Solution

Here given code implementation process.

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

void octahedralNo(int k)
{
	// Print all initial k octahedral number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		//   n(2n²+1)
		//  —————————
		//     3
      
		// Calculate nth octahedral number
		int result = (n *(2 *(n *n) + 1)) / 3;
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	//  Octahedral number are
	// —————————————————————————————————————————————
	//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	//  26214, 28595, 31116, 33781, 36594, 39559 etc.
  
	// k = 10
	octahedralNo(10);
	return 0;
}

Output

  1  6  19  44  85  146  231  344  489  670
// Java program for
// Octahedral number
public class OctahedralNumber
{
	public void octahedralNo(int k)
	{
		// Print all initial k octahedral number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			int result = (n * (2 * (n * n) + 1)) / 3;
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		OctahedralNumber task = new OctahedralNumber();
		//  Octahedral number are
		// —————————————————————————————————————————————
		//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
		//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
		//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
		//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
		//  26214, 28595, 31116, 33781, 36594, 39559 etc.
      
		// k = 10
		task.octahedralNo(10);
	}
}

Output

 1 6 19 44 85 146 231 344 489 670
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Octahedral number

class OctahedralNumber
{
	public: void octahedralNo(int k)
	{
		// Print all initial k octahedral number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			int result = (n *(2 *(n *n) + 1)) / 3;
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	OctahedralNumber *task = new OctahedralNumber();
	//  Octahedral number are
	// —————————————————————————————————————————————
	//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	//  26214, 28595, 31116, 33781, 36594, 39559 etc.
  
	// k = 10
	task->octahedralNo(10);
	return 0;
}

Output

 1 6 19 44 85 146 231 344 489 670
package main
import "fmt"
// Go program for
// Octahedral number

func octahedralNo(k int) {
	// Print all initial k octahedral number
	for n := 1 ; n <= k ; n++ {
		// Formula
		//   n(2n²+1)
		//  —————————
		//      3
		// Calculate nth octahedral number
		var result int = (n * (2 * (n * n) + 1)) / 3
      
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {

	//  Octahedral number are
	// —————————————————————————————————————————————
	//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	//  26214, 28595, 31116, 33781, 36594, 39559 etc.
  
	// k = 10
	octahedralNo(10)
}

Output

 1 6 19 44 85 146 231 344 489 670
// Include namespace system
using System;

// Csharp program for
// Octahedral number

public class OctahedralNumber
{
	public void octahedralNo(int k)
	{
		// Print all initial k octahedral number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			int result = (n * (2 * (n * n) + 1)) / 3;
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		OctahedralNumber task = new OctahedralNumber();
		//  Octahedral number are
		// —————————————————————————————————————————————
		//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
		//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
		//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
		//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
		//  26214, 28595, 31116, 33781, 36594, 39559 etc.
      
		// k = 10
		task.octahedralNo(10);
	}
}

Output

 1 6 19 44 85 146 231 344 489 670
<?php
// Php program for
// Octahedral number
class OctahedralNumber
{
	public	function octahedralNo($k)
	{
		// Print all initial k octahedral number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			$result = (int)(($n * (2 * ($n * $n) + 1)) / 3);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new OctahedralNumber();
	//  Octahedral number are
	// —————————————————————————————————————————————
	//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	//  26214, 28595, 31116, 33781, 36594, 39559 etc.
	// k = 10
	$task->octahedralNo(10);
}
main();

Output

 1 6 19 44 85 146 231 344 489 670
// Node JS program for
// Octahedral number
class OctahedralNumber
{
	octahedralNo(k)
	{
		// Print all initial k octahedral number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			var result = parseInt((n * (2 * (n * n) + 1)) / 3);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new OctahedralNumber();
	//  Octahedral number are
	// —————————————————————————————————————————————
	//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	//  26214, 28595, 31116, 33781, 36594, 39559 etc.
  
	// k = 10
	task.octahedralNo(10);
}
main();

Output

 1 6 19 44 85 146 231 344 489 670
#  Python 3 program for
#  Octahedral number
class OctahedralNumber :
	def octahedralNo(self, k) :
		n = 1
		#  Print all initial k octahedral number
		while (n <= k) :
			#  Formula
			#    n(2n²+1)
			#   —————————
			#      3
            
			#  Calculate nth octahedral number
			result = int((n * (2 * (n * n) + 1)) / 3)

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

def main() :
	task = OctahedralNumber()
	#   Octahedral number are
	#  —————————————————————————————————————————————
	#   1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	#   891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	#   4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	#   13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	#   26214, 28595, 31116, 33781, 36594, 39559 etc.
    
	#  k = 10
	task.octahedralNo(10)

if __name__ == "__main__": main()

Output

  1  6  19  44  85  146  231  344  489  670
#  Ruby program for
#  Octahedral number
class OctahedralNumber 
	def octahedralNo(k) 
		n = 1
		#  Print all initial k octahedral number
		while (n <= k) 
			#  Formula
			#    n(2n²+1)
			#   —————————
			#      3
            
			#  Calculate nth octahedral number
			result = (n * (2 * (n * n) + 1)) / 3

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

	end

end

def main() 
	task = OctahedralNumber.new()
	#   Octahedral number are
	#  —————————————————————————————————————————————
	#   1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	#   891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	#   4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	#   13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	#   26214, 28595, 31116, 33781, 36594, 39559 etc.
	#  k = 10
	task.octahedralNo(10)
end

main()

Output

 1 6 19 44 85 146 231 344 489 670
// Scala program for
// Octahedral number
class OctahedralNumber()
{
	def octahedralNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k octahedral number
		while (n <= k)
		{
			// Formula
          
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			var result: Int = (n * (2 * (n * n) + 1)) / 3;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: OctahedralNumber = new OctahedralNumber();
		//  Octahedral number are
		// —————————————————————————————————————————————
		//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
		//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
		//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
		//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
		//  26214, 28595, 31116, 33781, 36594, 39559 etc.
  
		// k = 10
		task.octahedralNo(10);
	}
}

Output

 1 6 19 44 85 146 231 344 489 670
// Swift 4 program for
// Octahedral number
class OctahedralNumber
{
	func octahedralNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k octahedral number
		while (n <= k)
		{
			// Formula
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			let result: Int = (n * (2 * (n * n) + 1)) / 3;
          
			// Display calculated result
			print(" ", result, terminator: "");
          
			n += 1;
		}
	}
}
func main()
{
	let task: OctahedralNumber = OctahedralNumber();
	//  Octahedral number are
	// —————————————————————————————————————————————
	//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	//  26214, 28595, 31116, 33781, 36594, 39559 etc.
	// k = 10
	task.octahedralNo(10);
}
main();

Output

  1  6  19  44  85  146  231  344  489  670
// Kotlin program for
// Octahedral number
class OctahedralNumber
{
	fun octahedralNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k octahedral number
		while (n <= k)
		{
			// Formula
			//   n(2n²+1)
			//  —————————
			//     3
          
			// Calculate nth octahedral number
			val result: Int = (n * (2 * (n * n) + 1)) / 3;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: OctahedralNumber = OctahedralNumber();
	//  Octahedral number are
	// —————————————————————————————————————————————
	//  1, 6, 19, 44, 85, 146, 231, 344, 489, 670, 
	//  891, 1156, 1469, 1834, 2255, 2736, 3281, 3894, 
	//  4579, 5340, 6181, 7106, 8119, 9224, 10425, 11726, 
	//  13131, 14644, 16269, 18010, 19871, 21856, 23969, 
	//  26214, 28595, 31116, 33781, 36594, 39559 etc.
	// k = 10
	task.octahedralNo(10);
}

Output

 1 6 19 44 85 146 231 344 489 670

Resultant Output Explanation

For k = 10, the first 10 octahedral numbers will be generated and displayed using the function octahedralNo(k). The output will be: 1 6 19 44 85 146 231 344 489 670

Time Complexity

The time complexity of this algorithm is O(k), where k is the number of octahedral numbers to be generated. Since we are using a loop to iterate from n = 1 to n = k and performing constant time operations within each iteration, the overall time complexity is directly proportional to k.

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