Posted on by Kalkicode
Code Number

Dodecahedral number

In mathematics, the concept of dodecahedral numbers is an intriguing and lesser-known topic. These numbers are part of a unique sequence with remarkable properties, forming a 3-dimensional figurate number. Figurate numbers are numbers that can be used to form regular geometric figures, and the dodecahedral numbers are no exception. They possess interesting geometric connections and can be derived using a specific formula.

Problem Statement

The task is to generate the first 'k' dodecahedral numbers and display them. The formula to calculate the nth dodecahedral number is given as:

 D_n = (n * (3n - 1) * (3n - 2)) / 2

where D_n is the nth dodecahedral number and 'n' is the position in the sequence.

Explanation with an Example

Let's understand the process of finding the dodecahedral numbers with 'k' = 5.

  1. For n = 0: D_0 = (0 * (30 - 1) * (30 - 2)) / 2 = 0

  2. For n = 1: D_1 = (1 * (31 - 1) * (31 - 2)) / 2 = 1

  3. For n = 2: D_2 = (2 * (32 - 1) * (32 - 2)) / 2 = 20

  4. For n = 3: D_3 = (3 * (33 - 1) * (33 - 2)) / 2 = 84

  5. For n = 4: D_4 = (4 * (34 - 1) * (34 - 2)) / 2 = 220

The first five dodecahedral numbers are 0, 1, 20, 84, and 220.

Pseudocode

Function dodecahedralNo(k)
    For n from 0 to k-1
        result = (n * (3n - 1) * (3n - 2)) / 2
        Print result
    End For
End Function

Algorithm Explanation

  1. Create a function dodecahedralNo(k) that takes an integer 'k' as input, which represents the number of dodecahedral numbers to be generated.

  2. Inside the function, iterate 'n' from 0 to k-1.

  3. For each 'n', calculate the corresponding dodecahedral number using the given formula.

  4. Print the calculated dodecahedral number.

  5. End the loop.

Code Solution

Here given code implementation process.

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

void dodecahedralNo(int k)
{
	// Print all initial k dodecahedral number
	for (int n = 0; n < k; ++n)
	{
		// Formula
		//  n(3n - 1) (3n - 2)
		//  ————————————————————
		//          2
      
		// Calculate nth dodecahedral number
		int result = (n *(3 *n - 1) *(3 *n - 2)) / 2;
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Dodecahedral number are
	// —————————————————————————————————————————————
	//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	//  59640, 67525, 76076, 85320, 95284, 105995, 
	//  117480, 129766, 142880, 156849 ... etc
  
	// Test k = 10
	dodecahedralNo(10);
	return 0;
}

Output

  0  1  20  84  220  455  816  1330  2024  2925
// Java program for
// Dodecahedral number
public class DodecahedralNumber
{
	public void dodecahedralNo(int k)
	{
		// Print all initial k dodecahedral number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			int result = (n * (3 * n - 1) * (3 * n - 2)) / 2;
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		DodecahedralNumber task = new DodecahedralNumber();
		// Dodecahedral number are
		// —————————————————————————————————————————————
		//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
		//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
		//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
		//  59640, 67525, 76076, 85320, 95284, 105995, 
		//  117480, 129766, 142880, 156849 ... etc
      
		// Test k = 10
		task.dodecahedralNo(10);
	}
}

Output

 0 1 20 84 220 455 816 1330 2024 2925
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Dodecahedral number

class DodecahedralNumber
{
	public: void dodecahedralNo(int k)
	{
		// Print all initial k dodecahedral number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			int result = (n *(3 *n - 1) *(3 *n - 2)) / 2;
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	DodecahedralNumber *task = new DodecahedralNumber();
	// Dodecahedral number are
	// —————————————————————————————————————————————
	//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	//  59640, 67525, 76076, 85320, 95284, 105995, 
	//  117480, 129766, 142880, 156849 ... etc
	// Test k = 10
	task->dodecahedralNo(10);
	return 0;
}

Output

 0 1 20 84 220 455 816 1330 2024 2925
// Include namespace system
using System;
// Csharp program for
// Dodecahedral number
public class DodecahedralNumber
{
	public void dodecahedralNo(int k)
	{
		// Print all initial k dodecahedral number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			int result = (n * (3 * n - 1) * (3 * n - 2)) / 2;
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		DodecahedralNumber task = new DodecahedralNumber();
		// Dodecahedral number are
		// —————————————————————————————————————————————
		//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
		//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
		//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
		//  59640, 67525, 76076, 85320, 95284, 105995, 
		//  117480, 129766, 142880, 156849 ... etc
      
		// Test k = 10
		task.dodecahedralNo(10);
	}
}

Output

 0 1 20 84 220 455 816 1330 2024 2925
package main
import "fmt"
// Go program for
// Dodecahedral number

func dodecahedralNo(k int) {
	// Print all initial k dodecahedral number
	for n := 0 ; n < k ; n++ {
		// Formula
		//  n(3n - 1) (3n - 2)
		//  ————————————————————
		//          2
		// Calculate nth dodecahedral number
		var result int = (n * (3 * n - 1) * 
							  (3 * n - 2)) / 2
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	
	// Dodecahedral number are
	// —————————————————————————————————————————————
	//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	//  59640, 67525, 76076, 85320, 95284, 105995, 
	//  117480, 129766, 142880, 156849 ... etc
	
	// Test k = 10
	dodecahedralNo(10)
}

Output

 0 1 20 84 220 455 816 1330 2024 2925
<?php
// Php program for
// Dodecahedral number
class DodecahedralNumber
{
	public	function dodecahedralNo($k)
	{
		// Print all initial k dodecahedral number
		for ($n = 0; $n < $k; ++$n)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			$result = (int)(($n * (3 * $n - 1) * (3 * $n - 2)) / 2);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new DodecahedralNumber();
	// Dodecahedral number are
	// —————————————————————————————————————————————
	//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	//  59640, 67525, 76076, 85320, 95284, 105995, 
	//  117480, 129766, 142880, 156849 ... etc
  
	// Test k = 10
	$task->dodecahedralNo(10);
}
main();

Output

 0 1 20 84 220 455 816 1330 2024 2925
// Node JS program for
// Dodecahedral number
class DodecahedralNumber
{
	dodecahedralNo(k)
	{
		// Print all initial k dodecahedral number
		for (var n = 0; n < k; ++n)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			var result = parseInt((n * (3 * n - 1) * 
                                   (3 * n - 2)) / 2);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new DodecahedralNumber();
	// Dodecahedral number are
	// —————————————————————————————————————————————
	//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	//  59640, 67525, 76076, 85320, 95284, 105995, 
	//  117480, 129766, 142880, 156849 ... etc
  
	// Test k = 10
	task.dodecahedralNo(10);
}
main();

Output

 0 1 20 84 220 455 816 1330 2024 2925
#  Python 3 program for
#  Dodecahedral number
class DodecahedralNumber :
	def dodecahedralNo(self, k) :
		n = 0
		#  Print all initial k dodecahedral number
		while (n < k) :
			#  Formula
			#   n(3n - 1) (3n - 2)
			#   ————————————————————
			#           2
            
			#  Calculate nth dodecahedral number
			result = int((n * (3 * n - 1) * (3 * n - 2)) / 2)

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

def main() :
	task = DodecahedralNumber()
	#  Dodecahedral number are
	#  —————————————————————————————————————————————
	#   0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	#   4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	#   20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	#   59640, 67525, 76076, 85320, 95284, 105995, 
	#   117480, 129766, 142880, 156849 ... etc
    
	#  Test k = 10
	task.dodecahedralNo(10)

if __name__ == "__main__": main()

Output

  0  1  20  84  220  455  816  1330  2024  2925
#  Ruby program for
#  Dodecahedral number
class DodecahedralNumber 
	def dodecahedralNo(k) 
		n = 0
		#  Print all initial k dodecahedral number
		while (n < k) 
			#  Formula
			#   n(3n - 1) (3n - 2)
			#   ————————————————————
			#           2
            
			#  Calculate nth dodecahedral number
			result = (n * (3 * n - 1) * (3 * n - 2)) / 2

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

def main() 
	task = DodecahedralNumber.new()
	#  Dodecahedral number are
	#  —————————————————————————————————————————————
	#   0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	#   4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	#   20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	#   59640, 67525, 76076, 85320, 95284, 105995, 
	#   117480, 129766, 142880, 156849 ... etc
    
	#  Test k = 10
	task.dodecahedralNo(10)
end

main()

Output

 0 1 20 84 220 455 816 1330 2024 2925
// Scala program for
// Dodecahedral number
class DodecahedralNumber()
{
	def dodecahedralNo(k: Int): Unit = {
		var n: Int = 0;
		// Print all initial k dodecahedral number
		while (n < k)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			var result: Int = (n * (3 * n - 1) * 
                               (3 * n - 2)) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DodecahedralNumber = new DodecahedralNumber();
		// Dodecahedral number are
		// —————————————————————————————————————————————
		//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
		//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
		//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
		//  59640, 67525, 76076, 85320, 95284, 105995, 
		//  117480, 129766, 142880, 156849 ... etc
  
		// Test k = 10
		task.dodecahedralNo(10);
	}
}

Output

 0 1 20 84 220 455 816 1330 2024 2925
// Swift 4 program for
// Dodecahedral number
class DodecahedralNumber
{
	func dodecahedralNo(_ k: Int)
	{
		var n: Int = 0;
		// Print all initial k dodecahedral number
		while (n < k)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			let result: Int = (n * (3 * n - 1) * (3 * n - 2)) / 2;
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: DodecahedralNumber = DodecahedralNumber();
	// Dodecahedral number are
	// —————————————————————————————————————————————
	//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	//  59640, 67525, 76076, 85320, 95284, 105995, 
	//  117480, 129766, 142880, 156849 ... etc
  
	// Test k = 10
	task.dodecahedralNo(10);
}
main();

Output

  0  1  20  84  220  455  816  1330  2024  2925
// Kotlin program for
// Dodecahedral number
class DodecahedralNumber
{
	fun dodecahedralNo(k: Int): Unit
	{
		var n: Int = 0;
		// Print all initial k dodecahedral number
		while (n < k)
		{
			// Formula
			//  n(3n - 1) (3n - 2)
			//  ————————————————————
			//          2
          
			// Calculate nth dodecahedral number
			val result: Int = (n * (3 * n - 1) * (3 * n - 2)) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: DodecahedralNumber = DodecahedralNumber();
	// Dodecahedral number are
	// —————————————————————————————————————————————
	//  0, 1, 20, 84, 220, 455, 816, 1330, 2024, 2925, 
	//  4060, 5456, 7140, 9139, 11480, 14190, 17296, 
	//  20825, 24804, 29260, 34220, 39711, 45760, 52394, 
	//  59640, 67525, 76076, 85320, 95284, 105995, 
	//  117480, 129766, 142880, 156849 ... etc
  
	// Test k = 10
	task.dodecahedralNo(10);
}

Output

 0 1 20 84 220 455 816 1330 2024 2925

Resultant Output Explanation:

With 'k' = 10, the function dodecahedralNo(10) generates the first 10 dodecahedral numbers and prints them as follows:

Output: 0 1 20 84 220 455 816 1330 2024 2925

The time complexity of the provided code is O(k), where 'k' is the number of dodecahedral numbers to be generated. This is because the code iterates 'n' from 0 to k-1 and performs constant time operations to calculate each dodecahedral number. As 'k' increases, the time taken by the algorithm grows linearly.

Conclusion

Dodecahedral numbers are a fascinating sequence of numbers with intricate connections to geometry and regular polyhedra. The provided code allows us to generate the first 'k' dodecahedral numbers and explore the pattern they exhibit. Understanding these mathematical patterns not only enhances our problem-solving skills but also deepens our appreciation for the beauty of mathematics.

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