main.swift101 chars5 lines
The pentagonalNumber
function takes an integer n
as input and calculates the nth
pentagonal number using the formula P_n = n(3n - 1) / 2
. The result is then returned as an integer.
For example, to find the 7th pentagonal number, we can call the function like this:
main.swift61 chars3 lines
The 7th pentagonal number is 92.
gistlibby LogSnag