# 实现斐波那契的第一种方法
def feibonaqi2(max):
n,a,b =0,0,1
listA=[]
while n<max:
listA.append(b)
a,b=b,a+b
return listA
listA=feibonaqi2(6)
for i in listA:
print(i)
# 实现斐波那契的第二种方法
def feibonaqi3(n):
if n==1:
return 1
elif n==2:
return 1
return feibonaqi3(n-1)+feibonaqi3(n-2)
print(feibonaqi3(6))
#第三种使用yield关键字
def feibonaqi5(max):
n, a, b = 0, 0, 1
while n < max:
yield b
# print b
a, b = b, a + b
n = n + 1
for i in feibonaqi5(5):
print(i)
python实现斐波那契数列
最新推荐文章于 2023-11-22 10:19:35 发布