标签:n+1 range code def == 斐波那契数列 end for col
斐波那契数列,别称黄金分割数列,兔子数列
定义:f(0) = 1,f(1) = 1,f(n) = f(n-1) + f(n-2)
例如:数列 1、1、2、3、5、8、13、21、……
#递归法 def fib(n): if n <= 0: return 1 elif n == 1: return n else: return fib(n-1) + fib(n-2) for i in range(0,30): print(fib(i), end = ‘ ‘) #O(1.618^n)
#循环法 def fib_loop(n): a, b = 0, 1 for i in range(n+1): a, b = b, a+b return a for i in range(0,30): print(fib(i), end = ‘ ‘) #O(n)
标签:n+1 range code def == 斐波那契数列 end for col
原文地址:https://www.cnblogs.com/hws2017/p/9949994.html