To find the nth triangular number in Swift, we can use the formula Tn = n(n+1)/2. Here's the Swift code that implements this formula:
main.swift129 chars7 lines
In this example, we pass 4
as the argument to the triangularNumber
function, which returns the 4th triangular number 10
.
The function simply calculates n * (n + 1)/2
and returns the result.
gistlibby LogSnag