Created
September 29, 2019 17:08
-
-
Save potatowagon/96508897914c3d69ab254d11ef73d156 to your computer and use it in GitHub Desktop.
Fibonacci sequence
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
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