Jun-06-2023, 10:34 AM
Griboullis Wrote:Barbones, but an example of algorithmic antipattern. Improve this with @functools.lru_cache perhaps. Use dynamic programming.From Python.org
from functools import lru_cache @lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) print([fib(n) for n in range(16)])
Output:[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]