def main(): n = 35 # Term to calculate print("> A recursive approach without optimizations:") if n < 36: res = time_it(fib1, n) write_result(res, n) else: print( f"fib({n}): Oops! It will take so much time, so I abort it. Sorry.\n{'-'*10}" ) print("> An iterative programming solution:") res = time_it(fib2, n) write_result(res, n) print("> A dynamic programming memoized solution\n" "for a recursive approach using a list:") res = time_it(fib3a, n) write_result(res, n) print( "> A dynamic programming memoized solution for a recursive approach\n" "using an array of unsigned integers of 64 or 32 bits:") res = time_it(fib3b, n) write_result(res, n) print("> A dynamic programming memoized solution using a dictionary:") res = time_it(fib4, n) write_result(res, n) print( "> A dynamic programming memoized solution for a recursive approach\n" "using functools.lru_cache from the standard library:") res = time_it(fib5, n) write_result(res, n) print( "> A dynamic programming memoized solution for a recursive approach\n" "using functools.lru_cache from the standard library.\n" "Benchmarks function fib with the user created decorator time_it:") res = fib6(n) write_result(res, n)
'''Calculates the n element of the Fibonacci sequence. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Where F(0) = 0, F(1) = 1 and F(n) = F(n - 1) + F(n - 2) for n > 1. So, each term greater than 1 is generated by adding the previous two terms. For example, the 6th element of the sequence is 8. It uses a dynamic programming memoized solution for a recursive approach using functools.lru_cache from the standard library. Benchmarks function fib with the user created function time_it. ''' __author__ = 'Joan A. Pinol (japinol)' from functools import lru_cache from fibonacci.time_it import time_it @lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n - 1) + fib(n - 2) if __name__ == "__main__": n = 35 res = time_it(fib, n) print(f'fib({n}): {res}')