non recursive fibonacci python code example
Example 1: python recursive fibonacci function
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Example 2: fibonacci recursive python
class recur_fibo:
memory = {0: 1, 1:1}
def fibonacci(n):
if n in recur_fibo.memory:
return recur_fibo.memory[n]
else:
recur_fibo.memory[n] = recur_fibo.fibonacci(n-1) + recur_fibo.fibonacci(n-2)
return recur_fibo.memory[n]
if __name__ == "__main__":
value = recur_fibo.fibonacci(200)
print(value)