find the nth decagonal number in python

To find the nth decagonal number, we can use the formula:

dn = 3n^2 - 2n

where n is the term number and dn is the decagonal number corresponding to that term.

Here's a Python function that implements this formula to find the nth decagonal number:

main.py
import math

def decagonal(n):
    """
    Returns the nth decagonal number using the formula dn = 3n^2 - 2n.
    """
    if n < 1:
        raise ValueError("n must be a positive integer")
    return 3 * n**2 - 2 * n

# example usage
print(decagonal(1))  # 1
print(decagonal(2))  # 10
print(decagonal(3))  # 27
print(decagonal(4))  # 52
337 chars
16 lines

gistlibby LogSnag