1 def fib_recur(n): 2 assert n >= 0 3 if n in (0, 1): 4 return n 5 return fib_recur(n - 1) + fib_recur(n - 2) 6 7 for i in range(20): 8 print(fib_recur(i), end=" ")
1 2)递推法 2 3 def fib_loop(n): 4 a, b = 0, 1 5 for i in range(n): 6 a, b = b, a + b 7 return a 8 9 for i in range(20): 10 print(fib_loop(i), end=" ")