Skip to content

Commit 861327d

Browse files
committed
Added memoization function in fibonacci
1 parent f4a16f6 commit 861327d

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

maths/fibonacci.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# fibonacci.py
22
"""
3-
Calculates the Fibonacci sequence using iteration, recursion, and a simplified
4-
form of Binet's formula
3+
Calculates the Fibonacci sequence using iteration, recursion, memoization,
4+
and a simplified form of Binet's formula
55
6-
NOTE 1: the iterative and recursive functions are more accurate than the Binet's
7-
formula function because the iterative function doesn't use floats
6+
NOTE 1: the iterative, recursive, memoization functions are more accurate than
7+
the Binet's formula function because the iterative function doesn't use floats
88
99
NOTE 2: the Binet's formula function is much more limited in the size of inputs
1010
that it can handle due to the size limitations of Python floats
1111
"""
1212

1313
from math import sqrt
1414
from time import time
15+
from typing import Callable
1516

1617

1718
def time_func(func, *args, **kwargs):
@@ -86,6 +87,35 @@ def fib_recursive_term(i: int) -> int:
8687
return [fib_recursive_term(i) for i in range(n + 1)]
8788

8889

90+
def fib_memoization() -> Callable[[int], int]:
91+
"""
92+
Calculates the nth fibonacci number using Memoization
93+
>>> fib_memoization()(5)
94+
5
95+
>>> fib_memoization()(100)
96+
354224848179261915075
97+
>>> fib_memoization()(25)
98+
75025
99+
>>> fib_memoization()(40)
100+
102334155
101+
"""
102+
# Cache must be outside recursuive function
103+
# other it will reset every time it calls itself.
104+
cache: dict[int, int] = {1: 1, 2: 1} # Prefilled cache
105+
106+
def rec_fn_memoized(num: int) -> int:
107+
if num < 1:
108+
raise Exception("n is negative")
109+
if num in cache:
110+
return cache[num]
111+
112+
value = rec_fn_memoized(num - 1) + rec_fn_memoized(num - 2)
113+
cache[num] = value
114+
return value
115+
116+
return rec_fn_memoized
117+
118+
89119
def fib_binet(n: int) -> list[int]:
90120
"""
91121
Calculates the first n (0-indexed) Fibonacci numbers using a simplified form
@@ -127,4 +157,5 @@ def fib_binet(n: int) -> list[int]:
127157
num = 20
128158
time_func(fib_iterative, num)
129159
time_func(fib_recursive, num)
160+
time_func(fib_memoization(), num)
130161
time_func(fib_binet, num)

0 commit comments

Comments
 (0)