find the nth nonagonal number in swift

To find the nth nonagonal number in Swift, we can use the formula:

Nn = n(7n-5)/2

where n is the index of the nonagonal number.

Here is an implementation of this function in Swift:

main.swift
func findNonagonalNumber(_ n: Int) -> Int {
    return n * (7 * n - 5) / 2
}
77 chars
4 lines

To use this function, simply call it with the index of the nonagonal number you want to find, like so:

main.swift
// Finds the 5th nonagonal number
let fifthNonagonalNumber = findNonagonalNumber(5)
print(fifthNonagonalNumber) // Output: 70
126 chars
4 lines

This will output 70, which is the 5th nonagonal number.

gistlibby LogSnag