Posted on by Kalkicode
Code Number

Stella octangula number

Stella Octangula numbers are a sequence of numbers that have interesting geometric and mathematical properties. The name "Stella Octangula" comes from Latin, where "Stella" means "Star," and "Octangula" refers to an octahedron, which is an eight-faced polyhedron. These numbers are obtained by applying a specific formula to generate the sequence.

Problem Statement

The task is to write a C program to generate the first 'k' Stella Octangula numbers using the given formula: n(2n^2 - 1). We'll display the sequence of Stella Octangula numbers up to the 'kth' term.

Explanation with Suitable Example

Let's take 'k = 5' as an example to illustrate how to generate the first five Stella Octangula numbers.

  1. For n = 0:

    • Stella Octangula number = 0(2 * 0^2 - 1) = 0 * (-1) = 0
  2. For n = 1:

    • Stella Octangula number = 1(2 * 1^2 - 1) = 1 * 1 = 1
  3. For n = 2:

    • Stella Octangula number = 2(2 * 2^2 - 1) = 2 * 7 = 14
  4. For n = 3:

    • Stella Octangula number = 3(2 * 3^2 - 1) = 3 * 17 = 51
  5. For n = 4:

    • Stella Octangula number = 4(2 * 4^2 - 1) = 4 * 31 = 124

So, the first five Stella Octangula numbers are {0, 1, 14, 51, 124}.

Pseudocode

stellaOctangulaNo(k):
    for n from 0 to k-1:
        result = n * (2 * n^2 - 1)
        print result

Algorithm Explanation

  1. Start the main function.
  2. Define the stellaOctangulaNo function that takes 'k' as input.
  3. Loop through 'n' from 0 to 'k-1': a. Calculate the nth Stella Octangula number using the formula: result = n * (2 * n^2 - 1). b. Print the calculated result.
  4. End the loop.
  5. End the function.
  6. In the main function, call the stellaOctangulaNo function with 'k' = 10.
  7. End the main function.

Code Solution

Here given code implementation process.

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

void stellaOctangulaNo(int k)
{
	// Print all initial k stella octangula number
	for (int n = 0; n < k; ++n)
	{
		//   Formula
		//   n(2n² - 1) 
      
		// Calculate nth stella octangula number
		int result = n *(2 *(n *n) - 1);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Stella octangula numbers are
	// —————————————————————————————————————————————
	//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	//  11646, 13699, 15980, 18501, 21274, 24311, 
	//  27624, 31225, 35126, 39339 ... etc
  
	//  Test  k = 10
	stellaOctangulaNo(10);
	return 0;
}

Output

  0  1  14  51  124  245  426  679  1016  1449
// Java program for
// Stella octangula number
public class StellaOctangula
{
	public void stellaOctangulaNo(int k)
	{
		// Print all initial k stella octangula number
		for (int n = 0; n < k; ++n)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			int result = n * (2 * (n * n) - 1);
          
			// Display calculated result
			System.out.print(" " + result );
		}
	}
	public static void main(String[] args)
	{
		StellaOctangula task = new StellaOctangula();
		// Stella octangula numbers are
		// —————————————————————————————————————————————
		//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
		//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
		//  11646, 13699, 15980, 18501, 21274, 24311, 
		//  27624, 31225, 35126, 39339 ... etc
      
		//  Test  k = 10
		task.stellaOctangulaNo(10);
	}
}

Output

 0 1 14 51 124 245 426 679 1016 1449
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Stella octangula number
class StellaOctangula
{
	public: void stellaOctangulaNo(int k)
	{
		// Print all initial k stella octangula number
		for (int n = 0; n < k; ++n)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			int result = n *(2 *(n *n) - 1);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	StellaOctangula *task = new StellaOctangula();
	// Stella octangula numbers are
	// —————————————————————————————————————————————
	//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	//  11646, 13699, 15980, 18501, 21274, 24311, 
	//  27624, 31225, 35126, 39339 ... etc
  
	//  Test  k = 10
	task->stellaOctangulaNo(10);
	return 0;
}

Output

 0 1 14 51 124 245 426 679 1016 1449
// Include namespace system
using System;
// Csharp program for
// Stella octangula number
public class StellaOctangula
{
	public void stellaOctangulaNo(int k)
	{
		// Print all initial k stella octangula number
		for (int n = 0; n < k; ++n)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			int result = n * (2 * (n * n) - 1);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		StellaOctangula task = new StellaOctangula();
		// Stella octangula numbers are
		// —————————————————————————————————————————————
		//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
		//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
		//  11646, 13699, 15980, 18501, 21274, 24311, 
		//  27624, 31225, 35126, 39339 ... etc
      
		//  Test  k = 10
		task.stellaOctangulaNo(10);
	}
}

Output

 0 1 14 51 124 245 426 679 1016 1449
package main
import "fmt"
// Go program for
// Stella octangula number

func stellaOctangulaNo(k int) {
	// Print all initial k stella octangula number
	for n := 0 ; n < k ; n++ {
		//   Formula
		//   n(2n² - 1) 

		// Calculate nth stella octangula number
		var result int = n * (2 * (n * n) - 1)
		
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {

	// Stella octangula numbers are
	// —————————————————————————————————————————————
	//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	//  11646, 13699, 15980, 18501, 21274, 24311, 
	//  27624, 31225, 35126, 39339 ... etc

	//  Test  k = 10
	stellaOctangulaNo(10)
}

Output

 0 1 14 51 124 245 426 679 1016 1449
<?php
// Php program for
// Stella octangula number
class StellaOctangula
{
	public	function stellaOctangulaNo($k)
	{
		// Print all initial k stella octangula number
		for ($n = 0; $n < $k; ++$n)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			$result = $n * (2 * ($n * $n) - 1);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new StellaOctangula();
	// Stella octangula numbers are
	// —————————————————————————————————————————————
	//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	//  11646, 13699, 15980, 18501, 21274, 24311, 
	//  27624, 31225, 35126, 39339 ... etc
  
	//  Test  k = 10
	$task->stellaOctangulaNo(10);
}
main();

Output

 0 1 14 51 124 245 426 679 1016 1449
// Node JS program for
// Stella octangula number
class StellaOctangula
{
	stellaOctangulaNo(k)
	{
		// Print all initial k stella octangula number
		for (var n = 0; n < k; ++n)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			var result = n * (2 * (n * n) - 1);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new StellaOctangula();
	// Stella octangula numbers are
	// —————————————————————————————————————————————
	//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	//  11646, 13699, 15980, 18501, 21274, 24311, 
	//  27624, 31225, 35126, 39339 ... etc
  
	//  Test  k = 10
	task.stellaOctangulaNo(10);
}
main();

Output

 0 1 14 51 124 245 426 679 1016 1449
#  Python 3 program for
#  Stella octangula number
class StellaOctangula :
	def stellaOctangulaNo(self, k) :
		n = 0
		#  Print all initial k stella octangula number
		while (n < k) :
			#    Formula
			#    n(2n² - 1) 
            
			#  Calculate nth stella octangula number
			result = n * (2 * (n * n) - 1)

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

def main() :
	task = StellaOctangula()
	#  Stella octangula numbers are
	#  —————————————————————————————————————————————
	#   0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	#   1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	#   11646, 13699, 15980, 18501, 21274, 24311, 
	#   27624, 31225, 35126, 39339 ... etc
    
	#   Test  k = 10
	task.stellaOctangulaNo(10)

if __name__ == "__main__": main()

Output

  0  1  14  51  124  245  426  679  1016  1449
#  Ruby program for
#  Stella octangula number
class StellaOctangula 
	def stellaOctangulaNo(k) 
		n = 0
		#  Print all initial k stella octangula number
		while (n < k) 
			#    Formula
			#    n(2n² - 1) 
            
			#  Calculate nth stella octangula number
			result = n * (2 * (n * n) - 1)

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

	end

end

def main() 
	task = StellaOctangula.new()
	#  Stella octangula numbers are
	#  —————————————————————————————————————————————
	#   0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	#   1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	#   11646, 13699, 15980, 18501, 21274, 24311, 
	#   27624, 31225, 35126, 39339 ... etc
    
	#   Test  k = 10
	task.stellaOctangulaNo(10)
end

main()

Output

 0 1 14 51 124 245 426 679 1016 1449
// Scala program for
// Stella octangula number
class StellaOctangula()
{
	def stellaOctangulaNo(k: Int): Unit = {
		var n: Int = 0;
		// Print all initial k stella octangula number
		while (n < k)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			var result: Int = n * (2 * (n * n) - 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: StellaOctangula = new StellaOctangula();
		// Stella octangula numbers are
		// —————————————————————————————————————————————
		//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
		//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
		//  11646, 13699, 15980, 18501, 21274, 24311, 
		//  27624, 31225, 35126, 39339 ... etc
  
		//  Test  k = 10
		task.stellaOctangulaNo(10);
	}
}

Output

 0 1 14 51 124 245 426 679 1016 1449
// Swift 4 program for
// Stella octangula number
class StellaOctangula
{
	func stellaOctangulaNo(_ k: Int)
	{
		var n: Int = 0;
		// Print all initial k stella octangula number
		while (n < k)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			let result: Int = n * (2 * (n * n) - 1);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: StellaOctangula = StellaOctangula();
	// Stella octangula numbers are
	// —————————————————————————————————————————————
	//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	//  11646, 13699, 15980, 18501, 21274, 24311, 
	//  27624, 31225, 35126, 39339 ... etc
  
	//  Test  k = 10
	task.stellaOctangulaNo(10);
}
main();

Output

  0  1  14  51  124  245  426  679  1016  1449
// Kotlin program for
// Stella octangula number
class StellaOctangula
{
	fun stellaOctangulaNo(k: Int): Unit
	{
		var n: Int = 0;
		// Print all initial k stella octangula number
		while (n < k)
		{
			//   Formula
			//   n(2n² - 1) 
          
			// Calculate nth stella octangula number
			val result: Int = n * (2 * (n * n) - 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: StellaOctangula = StellaOctangula();
	// Stella octangula numbers are
	// —————————————————————————————————————————————
	//  0, 1, 14, 51, 124, 245, 426, 679, 1016, 1449, 
	//  1990, 2651, 3444, 4381, 5474, 6735, 8176, 9809, 
	//  11646, 13699, 15980, 18501, 21274, 24311, 
	//  27624, 31225, 35126, 39339 ... etc
  
	//  Test  k = 10
	task.stellaOctangulaNo(10);
}

Output

 0 1 14 51 124 245 426 679 1016 1449

Resultant Output Explanation

When we run the C program with 'k = 10', the function stellaOctangulaNo(10) will generate the first 10 Stella Octangula numbers and display them as follows:

Output: 0 1 14 51 124 245 426 679 1016 1449

Time Complexity

The time complexity of the given code is O(k) because the loop runs 'k' times, and each iteration performs a constant amount of computation (calculating the nth Stella Octangula number). As 'k' increases, the time taken to execute the program will increase linearly.

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