Optimize Fibonacci with Caching
Medium
Write a function fast_fibonacci(n) that computes the nth Fibonacci number (0-indexed, fib(0)=0, fib(1)=1) using @functools.lru_cache to avoid redundant recursive calls.
Input
A non-negative integer n.
Output
The nth Fibonacci number as an integer.
Example
Input
fast_fibonacci(10)
Output
55
Apply @lru_cache(maxsize=None) above a recursive function: if n < 2 return n, else return fast_fibonacci(n-1) + fast_fibonacci(n-2).
15 XP on solve
Back to lesson
Your Solution
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to submit your solution and earn XP.