Skip to content

Added memoization function in fibonacci #5856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added memoization function in fibonacci
  • Loading branch information
JDeepD committed Nov 28, 2021
commit 861327d463c8371bb96d6b32762265b1372353c3
39 changes: 35 additions & 4 deletions maths/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# fibonacci.py
"""
Calculates the Fibonacci sequence using iteration, recursion, and a simplified
form of Binet's formula
Calculates the Fibonacci sequence using iteration, recursion, memoization,
and a simplified form of Binet's formula

NOTE 1: the iterative and recursive functions are more accurate than the Binet's
formula function because the iterative function doesn't use floats
NOTE 1: the iterative, recursive, memoization functions are more accurate than
the Binet's formula function because the iterative function doesn't use floats

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
"""

from math import sqrt
from time import time
from typing import Callable


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


def fib_memoization() -> Callable[[int], int]:
"""
Calculates the nth fibonacci number using Memoization
>>> fib_memoization()(5)
5
>>> fib_memoization()(100)
354224848179261915075
>>> fib_memoization()(25)
75025
>>> fib_memoization()(40)
102334155
"""
# Cache must be outside recursuive function
# other it will reset every time it calls itself.
cache: dict[int, int] = {1: 1, 2: 1} # Prefilled cache

def rec_fn_memoized(num: int) -> int:
if num < 1:
raise Exception("n is negative")
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


def fib_binet(n: int) -> list[int]:
"""
Calculates the first n (0-indexed) Fibonacci numbers using a simplified form
Expand Down Expand Up @@ -127,4 +157,5 @@ def fib_binet(n: int) -> list[int]:
num = 20
time_func(fib_iterative, num)
time_func(fib_recursive, num)
time_func(fib_memoization(), num)
time_func(fib_binet, num)