Now lets first make the standard recursive function.
deffibo(n:int)->int:# without memoization
ifn<=2:return1returnfibo(n-1)+fibo(n-2)
Now let's try to use memoization, that will reduce the time complexity to n.
defmemo_fibo(n:int,memo:dict={})->int:# with memoization
ifninmemo:returnmemo[n]ifn<=2:return1memo[n]=memo_fibo(n-1,memo)+memo_fibo(n-2,memo)returnmemo[n]
Let us set a value of n (the term of the Fibonacci sequence we want to find )
33.4 s ± 257 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)
Now let us execute the function that used the memo.
%%timeit-n1-r10memo_fibo(n)
The slowest run took 86.52 times longer than the fastest. This could mean that an intermediate result is being cached.
3.6 µs ± 9.59 µs per loop (mean ± std. dev. of 10 runs, 1 loop each)
The difference is stark. From 33.4 s to 3.6 µs.
Thus you see, how using memoization brings in a huge difference in time of execution.
The name 'Fibonacci' is due to a 13th-century Italian mathematician Leonardo of Pisa, who later came to be known as Fibonacci.
However, what we popularly call 'Fibonacci numbers' find their earliest mention in the 2nd century BCE work of Indian thinker Acharya Pingala.
Top comments (1)
or use the closed-form expression of the fibonacci sequence for constant time complexity