Created
October 19, 2013 14:18
-
-
Save lucasmcastro/7056491 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
#!/usr/bin/python | |
#-*- coding: utf8 -*- | |
import sys | |
def memoize(f): | |
cache = {} | |
def decorated_function(*args): | |
if args in cache: | |
return cache[args] | |
else: | |
cache[args] = f(*args) | |
return cache[args] | |
return decorated_function | |
@memoize | |
def fib(n): | |
return n if n < 2 else fib(n-2) + fib(n-1) | |
if __name__ == '__main__': | |
print fib(int(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment