Skip to content

Commit

Permalink
Create cached fibonacci algorithm (TheAlgorithms#8084)
Browse files Browse the repository at this point in the history
* feat: Add `fib_recursive_cached` func

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* doc: Show difference in time when caching algorithm

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Cjkjvfnby committed Mar 13, 2023
1 parent 2405a55 commit 7c234b2
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions maths/fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
fib_binet runtime: 0.0174 ms
"""

from functools import lru_cache
from math import sqrt
from time import time

Expand Down Expand Up @@ -92,6 +93,39 @@ def fib_recursive_term(i: int) -> int:
return [fib_recursive_term(i) for i in range(n + 1)]


def fib_recursive_cached(n: int) -> list[int]:
"""
Calculates the first n (0-indexed) Fibonacci numbers using recursion
>>> fib_iterative(0)
[0]
>>> fib_iterative(1)
[0, 1]
>>> fib_iterative(5)
[0, 1, 1, 2, 3, 5]
>>> fib_iterative(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> fib_iterative(-1)
Traceback (most recent call last):
...
Exception: n is negative
"""

@lru_cache(maxsize=None)
def fib_recursive_term(i: int) -> int:
"""
Calculates the i-th (0-indexed) Fibonacci number using recursion
"""
if i < 0:
raise Exception("n is negative")
if i < 2:
return i
return fib_recursive_term(i - 1) + fib_recursive_term(i - 2)

if n < 0:
raise Exception("n is negative")
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
Expand Down Expand Up @@ -163,8 +197,9 @@ def fib_binet(n: int) -> list[int]:


if __name__ == "__main__":
num = 20
num = 30
time_func(fib_iterative, num)
time_func(fib_recursive, num)
time_func(fib_recursive, num) # Around 3s runtime
time_func(fib_recursive_cached, num) # Around 0ms runtime
time_func(fib_memoization, num)
time_func(fib_binet, num)

0 comments on commit 7c234b2

Please sign in to comment.