- Recursive fibonacci number
# NO.8 fibonacci number, this method takes time and is inefficient
def fibo_recur(n):
if n <= 1:
return n
return fibo_recur(n - 1) + fibo_recur(n - 2)
fibo_recur(35)
- Iterative fibonacci number
def iterative_fibonacci(n): # fibonacci number, this method is better
if n < 2:
return n
previous, current = 0, 1
for _ in range(2, n + 1):
previous, current = current, previous + current
return current
iterative_fibonacci(35)
9227465