Posted on by Kalkicode
Code Number

# Decagonal number

Decagonal numbers are a sequence of figurate numbers that form a decagon when represented graphically. Each decagonal number represents the number of dots needed to form a regular decagon with sides made up of dots. In this article, we will explore the concept of decagonal numbers, understand the algorithm to calculate them, and implement a C program to generate the initial k decagonal numbers.

## Decagonal Numbers

Decagonal numbers can be visualized as dots arranged in a sequence of decagons. The first few decagonal numbers are: 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, and so on. The formula to calculate the nth decagonal number is given by: Dn = 4n^2 - 3n, where 'n' is the position of the decagonal number in the sequence.

Algorithm and Pseudocode: To generate the first k decagonal numbers, we will use a loop to iterate through the numbers from 0 to k-1 and calculate each decagonal number using the formula mentioned above. The pseudocode for the algorithm is as follows:

``````function decagonalNumber(k):
for n from 0 to k-1:
result = 4 * n^2 - 3 * n
print result
``````

## Explanation of Algorithm

1. We define a function `decagonalNumber` that takes an integer parameter `k`, representing the number of decagonal numbers we want to generate.
2. Inside the function, we use a loop that iterates through the numbers from 0 to k-1 (inclusive).
3. For each value of 'n' in the loop, we calculate the corresponding decagonal number using the formula Dn = 4n^2 - 3n and store it in the variable `result`.
4. We then print the value of `result`, which represents the nth decagonal number.

## Code Solution

Here given code implementation process.

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

void decagonalNo(int k)
{
// Print all initial k decagonal number
for (int n = 0; n < k; ++n)
{
// Formula
//  (4n² - 3n )
// Calculate nth decagonal number
int result = (4 *(n *n) - 3 *n);
// Display Calculated value
printf("  %d", result);
}
}
int main()
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
decagonalNo(10);
return 0;
}``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````// Java program for
// Decagonal number
public class DecagonalNumber
{
public void decagonalNo(int k)
{
// Print all initial k decagonal number
for (int n = 0; n < k; ++n)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
int result = (4 * (n * n) - 3 * n);

// Display calculated result
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
}
}``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Decagonal number

class DecagonalNumber
{
public: void decagonalNo(int k)
{
// Print all initial k decagonal number
for (int n = 0; n < k; ++n)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
int result = (4 *(n *n) - 3 *n);

// Display calculated result
cout << " " << result;
}
}
};
int main()
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
return 0;
}``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````// Include namespace system
using System;
// Csharp program for
// Decagonal number
public class DecagonalNumber
{
public void decagonalNo(int k)
{
// Print all initial k decagonal number
for (int n = 0; n < k; ++n)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
int result = (4 * (n * n) - 3 * n);

// Display calculated result
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
}
}``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````package main
import "fmt"
// Go program for
// Decagonal number

func decagonalNo(k int) {
// Print all initial k decagonal number
for n := 0 ; n < k ; n++ {
// Formula
//  (4n² - 3n )
// Calculate nth decagonal number
var result int = (4 * (n * n) - 3 * n)
// Display calculated result
fmt.Print(" ", result)
}
}
func main() {

// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc
// Test k = 10
decagonalNo(10)
}``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````<?php
// Php program for
// Decagonal number
class DecagonalNumber
{
public  function decagonalNo(\$k)
{
// Print all initial k decagonal number
for (\$n = 0; \$n < \$k; ++\$n)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
\$result = (4 * (\$n * \$n) - 3 * \$n);

// Display calculated result
echo(" ".\$result);
}
}
}

function main()
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
}
main();``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````// Node JS program for
// Decagonal number
class DecagonalNumber
{
decagonalNo(k)
{
// Print all initial k decagonal number
for (var n = 0; n < k; ++n)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
var result = (4 * (n * n) - 3 * n);

// Display calculated result
process.stdout.write(" " + result);
}
}
}

function main()
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
}
main();``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````#  Python 3 program for
#  Decagonal number
class DecagonalNumber :
def decagonalNo(self, k) :
n = 0
#  Print all initial k decagonal number
while (n < k) :
#  Formula
#   (4n² - 3n )

#  Calculate nth decagonal number
result = (4 * (n * n) - 3 * n)

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

def main() :
#  Decagonal number are
#  —————————————————————————————————————————————
#  0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
#  451, 540, 637, 742, 855, 976, 1105, 1242,
#  1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
#  2835, 3052, 3277, 3510, 3751, 4000, 4257,
#  4522, 4795, 5076, 5365, 5662 ... etc

#  Test k = 10

if __name__ == "__main__": main()``````

#### Output

``  0  1  10  27  52  85  126  175  232  297``
``````#  Ruby program for
#  Decagonal number
class DecagonalNumber
def decagonalNo(k)
n = 0
#  Print all initial k decagonal number
while (n < k)
#  Formula
#   (4n² - 3n )

#  Calculate nth decagonal number
result = (4 * (n * n) - 3 * n)

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

end

end

def main()
#  Decagonal number are
#  —————————————————————————————————————————————
#  0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
#  451, 540, 637, 742, 855, 976, 1105, 1242,
#  1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
#  2835, 3052, 3277, 3510, 3751, 4000, 4257,
#  4522, 4795, 5076, 5365, 5662 ... etc

#  Test k = 10
end

main()``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````// Scala program for
// Decagonal number
class DecagonalNumber()
{
def decagonalNo(k: Int): Unit = {
var n: Int = 0;
// Print all initial k decagonal number
while (n < k)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
var result: Int = (4 * (n * n) - 3 * n);

// Display calculated result
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: DecagonalNumber = new DecagonalNumber();
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
}
}``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``
``````// Swift 4 program for
// Decagonal number
class DecagonalNumber
{
func decagonalNo(_ k: Int)
{
var n: Int = 0;
// Print all initial k decagonal number
while (n < k)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
let result: Int = (4 * (n * n) - 3 * n);

// Display calculated result
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
}
main();``````

#### Output

``  0  1  10  27  52  85  126  175  232  297``
``````// Kotlin program for
// Decagonal number
class DecagonalNumber
{
fun decagonalNo(k: Int): Unit
{
var n: Int = 0;
// Print all initial k decagonal number
while (n < k)
{
// Formula
//  (4n² - 3n )

// Calculate nth decagonal number
val result: Int = (4 * (n * n) - 3 * n);

// Display calculated result
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Decagonal number are
// —————————————————————————————————————————————
// 0, 1, 10, 27, 52, 85, 126, 175, 232, 297, 370,
// 451, 540, 637, 742, 855, 976, 1105, 1242,
// 1387, 1540, 1701, 1870, 2047, 2232, 2425, 2626,
// 2835, 3052, 3277, 3510, 3751, 4000, 4257,
// 4522, 4795, 5076, 5365, 5662 ... etc

// Test k = 10
}``````

#### Output

`` 0 1 10 27 52 85 126 175 232 297``

## Output Explanation

When we run the above C program with `k = 10`, it will generate the first 10 decagonal numbers and print them on the screen. The output will be: `0 1 10 27 52 85 126 175 232 297`.

## Time Complexity

The time complexity of the given algorithm and C program is O(k), where 'k' is the number of decagonal numbers to be generated. This is because we are using a loop that runs 'k' times to calculate and print the decagonal numbers. The mathematical operations involved in calculating the decagonal numbers (multiplications and subtractions) take constant time, so they do not affect the overall time complexity.

Finally: In this article, we explored the concept of decagonal numbers, which are a sequence of figurate numbers forming a decagon. We learned the formula to calculate the nth decagonal number and implemented a C program to generate the first k decagonal numbers. The algorithm used a simple loop to calculate the numbers efficiently. The time complexity of the program is O(k), making it efficient for small values of 'k'. Decagonal numbers have various applications in mathematics and can be extended to explore other figurate numbers as well.

## 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.

Categories
Relative Post