From 6108260b9a4739017097d8ad2fb76cd4ddf1911b Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sat, 7 Jan 2023 13:00:02 +0000 Subject: [PATCH 1/3] feat: Add `fib_recursive_cached` func --- maths/fibonacci.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index e0da66ee5e3b..f8218655b2a7 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -18,6 +18,7 @@ from math import sqrt from time import time +from functools import lru_cache def time_func(func, *args, **kwargs): @@ -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 @@ -166,5 +200,6 @@ def fib_binet(n: int) -> list[int]: num = 20 time_func(fib_iterative, num) time_func(fib_recursive, num) + time_func(fib_recursive_cached, num) time_func(fib_memoization, num) time_func(fib_binet, num) From 22c8cd2e49e82a400b7b084eebb433f9b802d3fa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Jan 2023 13:03:23 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/fibonacci.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index f8218655b2a7..9f8e48fda2ed 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -16,9 +16,9 @@ fib_binet runtime: 0.0174 ms """ +from functools import lru_cache from math import sqrt from time import time -from functools import lru_cache def time_func(func, *args, **kwargs): From 9077bcfd31222048a2a8f5c6e3325818e9b3986c Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sat, 7 Jan 2023 16:01:50 +0000 Subject: [PATCH 3/3] doc: Show difference in time when caching algorithm --- maths/fibonacci.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 9f8e48fda2ed..d58c9fc68c67 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -197,9 +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_cached, 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)