Skip to content

Instantly share code, notes, and snippets.

@liketheflower
Created November 25, 2019 16:21
Show Gist options
  • Save liketheflower/8d1fed3578cddcf3e573f44373d0a3d6 to your computer and use it in GitHub Desktop.
Save liketheflower/8d1fed3578cddcf3e573f44373d0a3d6 to your computer and use it in GitHub Desktop.
fib
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