Created
September 13, 2011 05:15
-
-
Save faruken/1213172 to your computer and use it in GitHub Desktop.
fibonacci numbers with dynamic programming
This file contains 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(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