Skip to content
C

Cache a Slow Fibonacci with lru_cache

Mediumpython

Write a recursive Fibonacci function, apply @lru_cache to a second version, and confirm both agree on the answer and that a cached lookup is faster than the uncached computation.

Approach: define fibuncached (plain recursion) and fibcached (decorated with @lrucache), compute fib(n) with both, warm the cache, then time a fresh call to fibcached against the original uncached call.

Input: One line: an integer n.

Output: Three lines: "fib(<n>) = <value>", "Results match: True", and "Cache lookup faster than fresh computation: True".

Input (stdin)

Output

Run your code to see output here...