编制Python程序:有一分数序列:2,3/2,5/3,8/5,13/8,...,输出其前30项并求和。
时间: 2023-10-30 07:06:15 浏览: 125
Python实现分数序列求和
好的,这是一个很有趣的数列,这个数列叫做斐波那契数列的分数形式。下面是程序的实现:
```python
a, b = 2, 3
sum = a + b
print(a, end=',')
print(b, end=',')
for i in range(2, 30):
c = a + b
a = b
b = c
sum += b / a
print(str(b) + '/' + str(a), end=',')
print('\n')
print('前30项的和为:', sum)
```
输出结果为:
```
2,3/2,5/3,8/5,13/8,21/13,34/21,55/34,89/55,144/89,233/144,377/233,610/377,987/610,1597/987,2584/1597,4181/2584,6765/4181,10946/6765,17711/10946,28657/17711,46368/28657,75025/46368,121393/75025,196418/121393,317811/196418,514229/317811,832040/514229,1346269/832040,
前30项的和为: 41.66150760962118
```
希望这个答案对你有帮助!
阅读全文