nth_fibonacci

Ali Hamdi Ali Fadel · @AliOsm · about 4 years

Calculates the n-th Fibonacci number iteratively.

def nth_fibonacci(n)
n -= 2
# suppose that fibonacci is starts with 0, 1
a, b = 0, 1
n.times { a, b = b, a + b }
b
end
0 · 0 · 0