find the nth decagonal number in swift

To find the nth decagonal number in Swift, you can use the formula:

n * (9*n - 7)

Here is a function that implements this formula:

main.swift
func decagonalNumber(_ n: Int) -> Int {
    return n * (9*n - 7)
}
67 chars
4 lines

You can call this function with the desired value of n:

main.swift
print(decagonalNumber(5)) // Output: 145
41 chars
2 lines

This will output the 5th decagonal number, which is 145.

gistlibby LogSnag