
def fibonacci ( n ): # Check if input is valid if type ( n ) != int: We are able to do this without using recursion in the bottom-up approach. if n = 0 : return 0 if n <= 2 : return 1 # Recursive calls return fibonacci ( n - 1 ) + fibonacci ( n - 2 ) Bottom-Up Dynammic Programming Raise TypeError ( "n must be a positive integer!" ) if n < 1 : raise ValueError ( "n must be a positive integer!" ) # Base cases when n is 0, 1, or 2, the values are 0, 1, and 1. ( maxsize = 1000 ) def fibonacci ( n ): # Check if input is valid if type ( n ) != int: By default, python will cache 128 most receantly used values.

#=> Import lru_cache from functools module from functools import lru_cache #=> Enter the number of values to cache. LRU Cache stands for Least Recently Used Cache. To do this in a more clean way, we can use lru_cache module. fibonacci_cache = value #return it return value Raise TypeError ( "n must be a positive integer!" ) if n 2 : value = fibonacci ( n - 1 ) + fibonacci ( n - 2 ) # Cache the computed value. Raise TypeError ( "n must be a positive integer!" ) if n declare the dictionary that will store computed fibonacci values fibonacci_cache = def fibonacci ( n ): # Check if input is valid if type ( n ) != int: Therefore, when trying to compute F 300 it would take a great amount of time. Since this is a recursive approach, we first cover the base cases and then we make recursive calls. It repeats the same computations again and again. This approach has an exponential runtime complexity and it is too slow.
