Skip to content

Instantly share code, notes, and snippets.

@faruken
Created October 8, 2012 18:32
Show Gist options
  • Save faruken/3854090 to your computer and use it in GitHub Desktop.
Save faruken/3854090 to your computer and use it in GitHub Desktop.
Memoization - Fibonacci numbers example
def memoize(func):
cache = {}
def wrapper(*args):
try:
return cache[args]
except:
result = func(*args)
cache[args] = result
return result
return wrapper
@memoize
def fibonacci(n, counter, product):
if n == 0:
return product
else:
return fibonacci(n - 1, product, counter + product)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment