[python]练习之递归和循环实现斐波拉契数列
1 # 程序功能:用递归和循环实现斐波拉契数列 2 # 0 1 1 2 3 5 8 13 21 34 3 4 def digui_fibo(number): 5 if number == 1: 6 return 0 7 elif number == 2: 8 return 1 9 else: 10 return digui_fibo(number-1) + digui_fibo(number-2) 11 12 def loop_fibo(number): 13 first,second,value = 0,1,0 14 for i in range(1,number-1): 15 if number == 1: 16 value = 0 17 elif number == 2: 18 value = 1 19 else: 20 value = first + second 21 first = second 22 second = value 23 return value 24 25 26 print(digui_fibo(12)) 27 print(loop_fibo(12))