Skip to content

Instantly share code, notes, and snippets.

@john9631
Created October 26, 2013 02:17
Show Gist options
  • Save john9631/7164571 to your computer and use it in GitHub Desktop.
Save john9631/7164571 to your computer and use it in GitHub Desktop.
def memoize(f):
def mem(x):
if x not in cache:
cache[x] = f(x)
return cache[x]
cache = {}
return mem
#@memoize
def fib(n=1):
if n <= 1:
return 1
else:
return fib(n-1) + fib(n-2)
fib = memoize(fib)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment