Created
April 13, 2017 22:51
-
-
Save SergeyNarozhny/ad1067669b4f785082937ec6585263dc to your computer and use it in GitHub Desktop.
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 memo(f): | |
cache = {}; | |
def inner(n): | |
if n not in cache: | |
cache[n] = f(n); | |
return cache[n]; | |
return inner; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or using loop:
def fib3(n); assert n >= 0; f0, f1 = 0, 1; for i in range(n - 1): f0, f1 = f1, f0 + f1; return f1;