find the nth heptagonal number in swift

Here is a function that calculates the nth heptagonal number using the formula: n(5n-3)/2

main.swift
func heptagonalNumber(_ n: Int) -> Int {
    return n * (5 * n - 3) / 2
}
74 chars
4 lines

To use this function to find the nth heptagonal number, simply call the function with the desired value of n:

main.swift
let nthHeptagonalNumber = heptagonalNumber(5) // returns 95
60 chars
2 lines

This will give you the nth heptagonal number, where n is the input value passed to the function.

gistlibby LogSnag