Posted on by Kalkicode
Code Number

Dodecagonal number

Dodecagonal numbers are a unique subset of polygonal numbers, representing the number of dots that can be arranged to form a regular dodecagon (a 12-sided polygon). In this article, we will dive into the concept of dodecagonal numbers, understand their formula, and explore a C program to generate the first 'k' dodecagonal numbers.

Dodecagonal Numbers and Their Formula

Dodecagonal numbers are derived from a simple formula: Dn = 5n^2 - 4n, where 'n' is the nth term of the series. This formula is used to calculate the nth dodecagonal number by plugging in the value of 'n.'

Explanation with an Example

Let's understand the formula with an example. Consider 'n = 5'.
D5 = 5 * (5^2) - 4 * 5
D5 = 5 * 25 - 20
D5 = 125 - 20
D5 = 105
So, the 5th dodecagonal number is 105.

Pseudocode

To generate the first 'k' dodecagonal numbers, we can use the following pseudocode:

procedure dodecagonalNo(k)
    for n from 1 to k do
        result = 5 * n * n - 4 * n
        output result
    end for
end procedure

Algorithm Explanation

  1. Start the function 'dodecagonalNo(k)' with an input parameter 'k,' which represents the number of dodecagonal numbers to be generated.
  2. Initialize a loop that iterates from 'n = 1' to 'n = k.'
  3. Within the loop, calculate the dodecagonal number for the current 'n' using the formula 'result = 5 * n * n - 4 * n.'
  4. Output the calculated 'result' for each 'n.'
  5. End the loop.
  6. End the function.

Code Solution

Here given code implementation process.

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

void dodecagonalNo(int k)
{
	// Print all initial k dodecagonal number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		// (5n² - 4n )
      
		// Calculate nth dodecagonal number
		int result = (5 * (n * n) - 4 * n);
      
		// Display Calculated value
		printf("  %d", result);
	}
}
int main()
{
	// Dodecagonal number are
	// —————————————————————————————————————————————
	// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	// 5985, 6336, 6697, 7068 ... etc
  
	// Test k = 10
	dodecagonalNo(10);
	return 0;
}

Output

  1  12  33  64  105  156  217  288  369  460
// Java program for
// Dodecagonal number
public class DodecagonalNumber
{
	public void dodecagonalNo(int k)
	{
		// Print all initial k dodecagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (5n² - 4n )
          
			// Calculate nth dodecagonal number
			int result = (5 * (n * n) - 4 * n);
          
			// Display Calculated value
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		DodecagonalNumber task = new DodecagonalNumber();
		// Dodecagonal number are
		// —————————————————————————————————————————————
		// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
		// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
		// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
		// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
		// 5985, 6336, 6697, 7068 ... etc
      
		// Test k = 10
		task.dodecagonalNo(10);
	}
}

Output

 1 12 33 64 105 156 217 288 369 460
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Dodecagonal number

class DodecagonalNumber
{
	public: void dodecagonalNo(int k)
	{
		// Print all initial k dodecagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (5n² - 4n )
          
			// Calculate nth dodecagonal number
			int result = (5 *(n *n) - 4 *n);
          
			// Display Calculated value
			cout << " " << result;
		}
	}
};
int main()
{
	DodecagonalNumber *task = new DodecagonalNumber();
	// Dodecagonal number are
	// —————————————————————————————————————————————
	// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	// 5985, 6336, 6697, 7068 ... etc
  
	// Test k = 10
	task->dodecagonalNo(10);
	return 0;
}

Output

 1 12 33 64 105 156 217 288 369 460
// Include namespace system
using System;
// Csharp program for
// Dodecagonal number
public class DodecagonalNumber
{
	public void dodecagonalNo(int k)
	{
		// Print all initial k dodecagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (5n² - 4n )
          
			// Calculate nth dodecagonal number
			int result = (5 * (n * n) - 4 * n);
          
			// Display Calculated value
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		DodecagonalNumber task = new DodecagonalNumber();
		// Dodecagonal number are
		// —————————————————————————————————————————————
		// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
		// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
		// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
		// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
		// 5985, 6336, 6697, 7068 ... etc
      
		// Test k = 10
		task.dodecagonalNo(10);
	}
}

Output

 1 12 33 64 105 156 217 288 369 460
package main
import "fmt"
// Go program for
// Dodecagonal number

func dodecagonalNo(k int) {
	// Print all initial k dodecagonal number
	for n := 1 ; n <= k ; n++ {
		// Formula
		// (5n² - 4n )

		// Calculate nth dodecagonal number
		var result int = (5 * (n * n) - 4 * n)

		// Display Calculated value
		fmt.Print(" ", result)
	}
}
func main() {
	// Dodecagonal number are
	// —————————————————————————————————————————————
	// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	// 5985, 6336, 6697, 7068 ... etc
	
	// Test k = 10
	dodecagonalNo(10)
}

Output

 1 12 33 64 105 156 217 288 369 460
<?php
// Php program for
// Dodecagonal number
class DodecagonalNumber
{
	public	function dodecagonalNo($k)
	{
		// Print all initial k dodecagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			// (5n² - 4n )
          
			// Calculate nth dodecagonal number
			$result = (5 * ($n * $n) - 4 * $n);
          
			// Display Calculated value
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new DodecagonalNumber();
	// Dodecagonal number are
	// —————————————————————————————————————————————
	// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	// 5985, 6336, 6697, 7068 ... etc
  
	// Test k = 10
	$task->dodecagonalNo(10);
}
main();

Output

 1 12 33 64 105 156 217 288 369 460
// Node JS program for
// Dodecagonal number
class DodecagonalNumber
{
	dodecagonalNo(k)
	{
		// Print all initial k dodecagonal number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			// (5n² - 4n)
          
			// Calculate nth dodecagonal number
			var result = (5 * (n * n) - 4 * n);
          
			// Display Calculated value
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new DodecagonalNumber();
	// Dodecagonal number are
	// —————————————————————————————————————————————
	// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	// 5985, 6336, 6697, 7068 ... etc
	// Test k = 10
	task.dodecagonalNo(10);
}
main();

Output

 1 12 33 64 105 156 217 288 369 460
#  Python 3 program for
#  Dodecagonal number
class DodecagonalNumber :
	def dodecagonalNo(self, k) :
		n = 1
		#  Print all initial k dodecagonal number
		while (n <= k) :
			#  Formula
			#  (5n² - 4n)
            
			#  Calculate nth dodecagonal number
			result = (5 * (n * n) - 4 * n)

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

def main() :
	task = DodecagonalNumber()
	#  Dodecagonal number are
	#  —————————————————————————————————————————————
	#  1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	#  672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	#  1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	#  3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	#  5985, 6336, 6697, 7068 ... etc
    
	#  Test k = 10
	task.dodecagonalNo(10)

if __name__ == "__main__": main()

Output

  1  12  33  64  105  156  217  288  369  460
#  Ruby program for
#  Dodecagonal number
class DodecagonalNumber 
	def dodecagonalNo(k) 
		n = 1
		#  Print all initial k dodecagonal number
		while (n <= k) 
			#  Formula
			#  (5n² - 4n)
            
			#  Calculate nth dodecagonal number
			result = (5 * (n * n) - 4 * n)

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

	end

end

def main() 
	task = DodecagonalNumber.new()
	#  Dodecagonal number are
	#  —————————————————————————————————————————————
	#  1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	#  672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	#  1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	#  3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	#  5985, 6336, 6697, 7068 ... etc
    
	#  Test k = 10
	task.dodecagonalNo(10)
end

main()

Output

 1 12 33 64 105 156 217 288 369 460
// Scala program for
// Dodecagonal number
class DodecagonalNumber()
{
	def dodecagonalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k dodecagonal number
		while (n <= k)
		{
			// Formula
			// (5n² - 4n)
          
			// Calculate nth dodecagonal number
			var result: Int = (5 * (n * n) - 4 * n);
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DodecagonalNumber = new DodecagonalNumber();
		// Dodecagonal number are
		// —————————————————————————————————————————————
		// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
		// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
		// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
		// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
		// 5985, 6336, 6697, 7068 ... etc
  
		// Test k = 10
		task.dodecagonalNo(10);
	}
}

Output

 1 12 33 64 105 156 217 288 369 460
// Swift 4 program for
// Dodecagonal number
class DodecagonalNumber
{
	func dodecagonalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k dodecagonal number
		while (n <= k)
		{
			// Formula
			// (5n² - 4n)
          
			// Calculate nth dodecagonal number
			let result: Int = (5 * (n * n) - 4 * n);
          
			// Display Calculated value
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: DodecagonalNumber = DodecagonalNumber();
	// Dodecagonal number are
	// —————————————————————————————————————————————
	// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	// 5985, 6336, 6697, 7068 ... etc
  
	// Test k = 10
	task.dodecagonalNo(10);
}
main();

Output

  1  12  33  64  105  156  217  288  369  460
// Kotlin program for
// Dodecagonal number
class DodecagonalNumber
{
	fun dodecagonalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k dodecagonal number
		while (n <= k)
		{
			// Formula
			// (5n² - 4n)
          
			// Calculate nth dodecagonal number
			val result: Int = (5 * (n * n) - 4 * n);
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: DodecagonalNumber = DodecagonalNumber();
	// Dodecagonal number are
	// —————————————————————————————————————————————
	// 1, 12, 33, 64, 105, 156, 217, 288, 369, 460, 561, 
	// 672, 793, 924, 1065, 1216, 1377, 1548, 1729, 
	// 1920, 2121, 2332, 2553, 2784, 3025, 3276, 3537, 
	// 3808, 4089, 4380, 4681, 4992, 5313, 5644, 
	// 5985, 6336, 6697, 7068 ... etc
  
	// Test k = 10
	task.dodecagonalNo(10);
}

Output

 1 12 33 64 105 156 217 288 369 460

Time Complexity

The time complexity of this algorithm is O(k) because the loop iterates 'k' times to generate the 'k' dodecagonal numbers. The calculations inside the loop are constant time operations, so they do not significantly affect the overall time complexity.

Output Explanation

When we test the function with 'k = 10' (dodecagonalNo(10)), it will generate the first 10 dodecagonal numbers and print them as follows: 1 12 33 64 105 156 217 288 369 460

Finally

Dodecagonal numbers are an interesting sequence of polygonal numbers that have various applications in mathematics and other fields. In this article, we explored the concept of dodecagonal numbers, their formula, and a C program to generate the first 'k' dodecagonal numbers. We also provided a detailed explanation of the algorithm and the time complexity of the code. By understanding dodecagonal numbers, we gain valuable insights into mathematical patterns and relationships.

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