Created
October 18, 2011 07:23
-
-
Save tmiz/1294810 to your computer and use it in GitHub Desktop.
cached fib
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
cache_fib={} | |
def c_fib(n): | |
if cache_fib.has_key(n): | |
return cache_fib[n] | |
if n==0: | |
cache_fib[0]=0 | |
elif n==1: | |
cache_fib[1]=1 | |
else: | |
cache_fib[n] = c_fib(n-1)+c_fib(n-2) | |
return cache_fib[n] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment