python多个斐波那契
时间: 2023-10-13 10:19:34 浏览: 78
以下是使用Python编写的多个斐波那契数列的代码示例:
1. 生成前n个斐波那契数列:
```python
def fibonacci_sequence(n):
a, b = 0, 1
for i in range(n):
yield a
a, b = b, a + b
n = 10
for num in fibonacci_sequence(n):
print(num)
```
2. 生成指定范围内的斐波那契数列:
```python
def fibonacci_sequence_range(start, end):
a, b = 0, 1
while a <= end:
if a >= start:
yield a
a, b = b, a + b
start, end = 0, 100
for num in fibonacci_sequence_range(start, end):
print(num)
```
3. 生成第n个斐波那契数:
```python
def fibonacci_number(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci_number(n-1) + fibonacci_number(n-2)
n = 10
print(fibonacci_number(n))
```
4. 生成小于等于n的最大斐波那契数:
```python
def max_fibonacci_number(n):
a, b = 0, 1
while b <= n:
a, b = b, a + b
return a
n = 100
print(max_fibonacci_number(n))
```
阅读全文