I am a beginner to python, and was following this video for learning dynamic programming. For the case of finding a Fibonacci series, the tutor introduced memoization to improve the performance of the recursive algorithm. The complete code is as follows
import time
def fibonacci1(n):
'''This uses the naive fibonacci algorithm. '''
if n < 2:
return n
else:
return fibonacci1(n-1) + fibonacci1(n-2)
def fibonacci2(n):
''' This function uses a memo of computed values to speed up the process'''
memo = {}
if n in memo:
return memo[n]
if n < 2:
return n
f = fibonacci2(n-1) + fibonacci2(n-2)
memo[n] = f
return f
def screen(i,N):
if i==1:
return fibonacci1(N)
if i == 2:
return fibonacci2(N)
N = int(input("Enter a number: "))
for i in range(2):
t0 = time.time()
fib = screen(i+1,N)
print("The "+ str(N) +"th fibonacci number is {}".format(fib))
t1 = time.time()
print("Time taken is : {}s".format(t1-t0))
But this is the output I received: FibonacciOutput
Can somebody help me with this?