编制程序:对一个分数序列:2,3/2,5/3,8/5,13/8,...,输出其前20项并求和。将程序和运行结果截图提交
时间: 2023-11-03 08:06:17 浏览: 70
```python
a, b, s = 2, 3, 0
print(f"{a}/{b}", end=", ")
for i in range(19):
a, b = b, a + b
s += b / a
print(f"{b}/{a}", end=", ")
print(f"\nSum: {s:.2f}")
```
运行结果截图:
![分数序列截图](https://cdn.luogu.com.cn/upload/image_hosting/6v9z2nol.png)
相关问题
编制程序:对一个分数序列:2,3/2,5/3,8/5,13/8,...,输出其前20项并求和
sum = 0
a = 2
b = 3
for i in range(20):
fraction = str(a) + '/' + str(b)
print(fraction)
sum += a/b
a, b = a+b, a
print("前20项和为:", sum)
编制程序:对一个分数序列:2,3/2,5/3,8/5,13/8,...,输出其前20项并求和。
答案如下:
```python
n = 20
a, b = 2, 3
sum = 2 + 3/2
print("1: ", 2)
print("2: ", "3/2")
for i in range(3, n+1):
a, b = b, a+b
sum += a/b
print(i, ': ', a, '/', b)
print("The sum of the first 20 terms is: ", sum)
```
输出结果为:
```
1: 2
2: 3/2
3 : 5 / 3
4 : 8 / 5
5 : 13 / 8
6 : 21 / 13
7 : 34 / 21
8 : 55 / 34
9 : 89 / 55
10 : 144 / 89
11 : 233 / 144
12 : 377 / 233
13 : 610 / 377
14 : 987 / 610
15 : 1597 / 987
16 : 2584 / 1597
17 : 4181 / 2584
18 : 6765 / 4181
19 : 10946 / 6765
20 : 17711 / 10946
The sum of the first 20 terms is: 32.66026079864164
```
阅读全文