Fibonacci numbers with memoization runs slow in Python? -
def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) def memo(f): cache = {} def memoized(n): if n not in cache: cache[n] = f(n) return cache[n] return memoized fib1 = memo(fib)
this code runs slow on laptop, if change name fib1 fib, works fine...anyone know reason ? thanks!
fib
recurses fib
, not fib1
. if memoized version has different name won't used.
Comments
Post a Comment