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

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -