Python活力练习Day8
Day8:斐波那契数列
eg : [1,1,2,3,5,8,...]
方法一:列表来存储
1 def feib(n): 2 f = [1,1] 3 f[0] == 1 4 f[1] == 1 5 i = 2 6 while i < n: 7 s = f[i-1] + f[i-2] 8 f.append(s) 9 i += 1 10 return f 11 12 n = 10 13 print(feib(n))
方法二:
1 def feib(n): 2 a,b = 1,1 3 print(a,end = " ") 4 '''for i in range(n-1): 5 a,b = b, a+b''' 6 while n-1: 7 a , b ,n = b , a+b , n-1 8 print(a,end = " ") 9 10 n = 10 11 feib(n)
输出结果: