-
-
Save stoensin/ceb149bbe2b29b12f94d648b15434d3c to your computer and use it in GitHub Desktop.
斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 如果设F(n)为该数列的第n项(n∈N*),那么这句话可以写成如下形式::F(n)=F(n-1)+F(n-2) 显然这是一个线性递推数列
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
def fib_iter(n): | |
pre,next = 1, 1 | |
result = 0 | |
i = 2 | |
while i < n: | |
result = pre + next | |
pre,next= next,result | |
i += 1 | |
return result | |
def fab(max): | |
n, a, b = 0, 0, 1 | |
L = [] | |
while n < max: | |
L.append(b) | |
a, b = b, a + b | |
n += 1 | |
return L | |
def fibonacci3(num): | |
a, b = 0, 1 | |
for i in range(1, num+1): | |
a, b = b, a+b | |
return b | |
def fab(num): | |
n, a, b = 0, 0, 1 | |
while n < num: | |
yield b | |
# print b | |
a, b = b, a + b | |
n += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment