Skip to content

Instantly share code, notes, and snippets.

@varundey
Created March 10, 2017 07:37
Show Gist options
  • Save varundey/af7df02cf3bb3193f17a2b0b6852c3d1 to your computer and use it in GitHub Desktop.
Save varundey/af7df02cf3bb3193f17a2b0b6852c3d1 to your computer and use it in GitHub Desktop.
Python program to print the nth fibonacci number
def memoize(f):
memo = {}
def helper(x):
if x not in memo:
memo[x] = f(x)
return memo[x]
return helper
@memoize
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print(fib(40))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment