Created
October 26, 2013 02:17
-
-
Save john9631/7164571 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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