Created
April 25, 2019 15:01
-
-
Save nickyfoto/455b87ca3bcaefd710e8b3d18863da77 to your computer and use it in GitHub Desktop.
Calculate nth Fibonacci number using memoization
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
def fibM(n, d={0: 0, 1: 1}): | |
"""Memoization Fib""" | |
if n-2 in d and n-1 in d: | |
return d[n-2] + d[n-1] | |
else: | |
d[n-1] = d[n-3] + d[n-2] | |
return fibM(n-1, d) | |
fib(25) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment