1. 斐波那契数列
写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项。计算结果取模 1e9+7(1000000007)
方法一:
def fib(n):
if n <= 2:
return 0 if n == 0 else 1
else:
return (fib(n - 1) + fib(n - 2)) % 1000000007
方法二:
def fib(n):
a, b = 0, 1
for _ in range(n):
sum_val = (a + b) % 1000000007
a, b = b, sum_val
return a