# 斐波那契数列第n项
# 1 1 2 3 5 8 def fib(n):
if n <= 2:
return 1
else:
return fib(n-2)+fib(n-1) def fib2(n):
if n < 3:
return 1
f1 = f2 = 1
for k in range(1, n-1):
f1, f2 = f2, f2+f1
return f2 if __name__ == '__main__':
# 1 1 2 3 5 8 13
print(fib(6)) #
print(fib2(6)) #
05-17 13:40