Created
January 4, 2013 09:21
-
-
Save peterldowns/4451167 to your computer and use it in GitHub Desktop.
Simple memoized fibonacci number generator. Works because Python evaluates default argument variables at the time of definition, not call.
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 recursive_fib(n, cache={}): | |
if n <= 1: | |
return 1 | |
cache[n] = cache.get(n) or n * recursive_fib(n - 1, cache) | |
return cache[n] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment