用python求前四十个斐波那契数

#求前40个斐波那契数
#方法1
L=[]
a=0
b=1
n=0
while n<40:
    L+=[b]
    a,b=b,a+b
    n+=1
print("前四十个斐波那契数有:",L)
print("----------------")

#方法2
L=[]
for s in range(40):
    if s==0 or s==1:
        L.append(1)
    else:
        L.append(L[s-2]+L[s-1])   #从第三项开始每项值为前两项值之和
print("前四十个斐波那契数有:",L)
print("------------------")

#方法3
L=[1,1]
while len(L)<40:
    L.append(L[-1]+L[-2])
print("前四十个斐波那契数有:",L)

你可能感兴趣的:(Python)