Hey folks! In this tutorial, we’ll explore how to obtain the nth Decagonal Number in Python. But first, let’s go over the basics of Decagonal Numbers.
Introduction to Decagonal Number
A decagonal number is a figurate number that extends the notion of the triangle and square numbers to the decagon (a ten-sided polygon). The nth decagonal number counts the number of dots in a pattern of n nested decagons, each of which has a shared corner.
The following formula generates the n-th decagonal number:
D(n) = 4*n^2 – 3*n
Algorithm to find nth Decagonal Number
In order to obtain the nth Decagonal Number using the Python programming language, we will be following the steps mentioned below:
- Take the input for the value of n
- Compute the value of D(n) using the formula mentioned in the previous section.
- Display the computed value of D(n)
Finding the nth decagonal number with Python
def GetDecagonalNumber(n):
return (4*n*n - 3*n)
n = int(input("Enter the value of n: "))
print("The required Decagonal Number is: ", GetDecagonalNumber(n))
Here, we have created a function to make things simpler to understand and generalize a direct function that can be reused in any other code.
Sample Outputs
Enter the value of n: 3
The required Decagonal Number is: 27
Enter the value of n: 20
The required Decagonal Number is: 1540
Conclusion
Congratulations! You just learned how to compute nth Decagonal number in the Python programming language. Hope you enjoyed it! 😇
Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:
Thank you for taking your time out! Hope you learned something new!! 😄