Skip to content

Instantly share code, notes, and snippets.

@potatowagon
Created September 29, 2019 17:08
Show Gist options
  • Save potatowagon/96508897914c3d69ab254d11ef73d156 to your computer and use it in GitHub Desktop.
Save potatowagon/96508897914c3d69ab254d11ef73d156 to your computer and use it in GitHub Desktop.
Fibonacci sequence
import pytest
'''output nth (starting from 1) sequence of fibonacci series: 0,1,1,2,3,5,8. n = 7, output = 8'''
def fib(n):
seq = [0, 1] + [0] * (n - 2)
for i in range(2, n):
# dynamic programming
seq[i] = seq[i-1] + seq[i-2]
return seq[n-1]
def test_fib():
assert fib(1) == 0
assert fib(7) == 8
for i in range(1, 50):
print(fib(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment