Added memoization function in fibonacci (#5856)

* Added memoization function in fibonacci

* Minor changes
This commit is contained in:
Jaydeep Das 2021-11-28 23:50:18 +05:30 committed by GitHub
parent f4a16f607b
commit 65d3cfff2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,19 @@
# fibonacci.py # fibonacci.py
""" """
Calculates the Fibonacci sequence using iteration, recursion, and a simplified Calculates the Fibonacci sequence using iteration, recursion, memoization,
form of Binet's formula and a simplified form of Binet's formula
NOTE 1: the iterative and recursive functions are more accurate than the Binet's NOTE 1: the iterative, recursive, memoization functions are more accurate than
formula function because the iterative function doesn't use floats the Binet's formula function because the Binet formula function uses floats
NOTE 2: the Binet's formula function is much more limited in the size of inputs NOTE 2: the Binet's formula function is much more limited in the size of inputs
that it can handle due to the size limitations of Python floats that it can handle due to the size limitations of Python floats
RESULTS: (n = 20)
fib_iterative runtime: 0.0055 ms
fib_recursive runtime: 6.5627 ms
fib_memoization runtime: 0.0107 ms
fib_binet runtime: 0.0174 ms
""" """
from math import sqrt from math import sqrt
@ -86,6 +92,39 @@ def fib_recursive(n: int) -> list[int]:
return [fib_recursive_term(i) for i in range(n + 1)] return [fib_recursive_term(i) for i in range(n + 1)]
def fib_memoization(n: int) -> list[int]:
"""
Calculates the first n (0-indexed) Fibonacci numbers using memoization
>>> fib_memoization(0)
[0]
>>> fib_memoization(1)
[0, 1]
>>> fib_memoization(5)
[0, 1, 1, 2, 3, 5]
>>> fib_memoization(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> fib_iterative(-1)
Traceback (most recent call last):
...
Exception: n is negative
"""
if n < 0:
raise Exception("n is negative")
# Cache must be outside recursuive function
# other it will reset every time it calls itself.
cache: dict[int, int] = {0: 0, 1: 1, 2: 1} # Prefilled cache
def rec_fn_memoized(num: int) -> int:
if num in cache:
return cache[num]
value = rec_fn_memoized(num - 1) + rec_fn_memoized(num - 2)
cache[num] = value
return value
return [rec_fn_memoized(i) for i in range(n + 1)]
def fib_binet(n: int) -> list[int]: def fib_binet(n: int) -> list[int]:
""" """
Calculates the first n (0-indexed) Fibonacci numbers using a simplified form Calculates the first n (0-indexed) Fibonacci numbers using a simplified form
@ -127,4 +166,5 @@ if __name__ == "__main__":
num = 20 num = 20
time_func(fib_iterative, num) time_func(fib_iterative, num)
time_func(fib_recursive, num) time_func(fib_recursive, num)
time_func(fib_memoization, num)
time_func(fib_binet, num) time_func(fib_binet, num)