Simple usage on Fibonacci
Input
fib(6)
Output
8
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5, fib(6)=8. The memoize decorator caches intermediate results so computing fib(6) is fast.
Full lesson preview
Create a decorator that caches function return values to avoid repeated work. Apply it to a recursive function to improve performance.
Problem statement
Task
Examples
Input
fib(6)
Output
8
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5, fib(6)=8. The memoize decorator caches intermediate results so computing fib(6) is fast.
Input format
Output format
Constraints
Samples
Input
fib(5)
Output
5
Returns the 5th Fibonacci number (0-indexed) which is 5.