斐波那契数python求法
时间: 2023-11-30 10:43:06 浏览: 85
斐波那契数python
以下是两种Python求斐波那契数列的方法:
方法一:使用循环
```python
n = int(input())
a, b = 0, 1
while b <= n:
a, b = b, a + b
print(b)
```
方法二:使用递归
```python
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
n = int(input())
i = 0
while fibonacci(i) <= n:
i += 1
print(fibonacci(i))
```
阅读全文