Created
November 25, 2019 16:21
-
-
Save liketheflower/8d1fed3578cddcf3e573f44373d0a3d6 to your computer and use it in GitHub Desktop.
fib
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
from functools import lru_cache | |
@lru_cache(None) | |
def fib_top_down(n): | |
if n==0: return 0 | |
if n==1: return 1 | |
return fib_top_down(n-1)+fib_top_down(n-2) | |
def fib_bottom_up(n): | |
if n==0: return 0 | |
if n==1: return 1 | |
a, b = 0, 1 | |
for i in range(2, n+1): | |
c = a + b | |
a, b = b, c | |
return c | |
for n in range(10): | |
print(fib_top_down(n)) | |
print(fib_bottom_up(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment