def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while b < n: print(b) a, b = b, a+b >>>fib(2000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 我们可以编写一个函数来生成有给…
1,2,3,5,8,13..... 求第n个数 def get_num(n): if n == 1: return 1 if n == 2: return 2 return get_num(n - 1) + get_num(n - 2) def get_num(n): if n == 1: return 1 if n == 2: return 2 a = 1 b = 2 for i in range(n - 2): a, b = b, a + b return b分为递归和循环两个求法…