Created
January 26, 2017 19:51
-
-
Save amiiy/23383546da1d6b6f0017118b71e2f240 to your computer and use it in GitHub Desktop.
fibonacci in dynamic programing way
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
#fibonacci series with dynamic programing Approach ! | |
#better way ... | |
def fib(n): | |
l = [1,1] | |
if n == 0 : | |
return 1 | |
elif n == 1 : | |
return 1 | |
elif n == 2 : | |
return l | |
elif n>2 : | |
temp = 0 | |
for i in range(n-2): | |
temp = l[-2] + l[-1] | |
l.append(temp) | |
return l | |
print(fib(1)) | |
print(fib(2)) | |
print(fib(3)) | |
print(fib(12)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment