Skip to content

Instantly share code, notes, and snippets.

@peterldowns
Created January 4, 2013 09:21
Show Gist options
  • Save peterldowns/4451167 to your computer and use it in GitHub Desktop.
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.
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