1、斐波那契数列(两个元素的和确定下一个数)
1 # 1,1,2,3,5,8,13,21,34,55,89...... 2 # 方法一:递归 3 def feb(n): 4 if n == 1 or n == 2: 5 return 1 6 else: 7 return feb(n-1)+feb(n-2) 8 n = 8 9 print(feb(n))10 11 12 # 方法二:循环13 x,y = 0,014 n = 815 for i in range(0,n+1):16 if i == 1 or i == 2:17 x,y = 1,118 else:19 x,y = y,x+y20 print(y)