编写代码求分数序列: 求出这个数列2/1,-3/2,5/3,-13/5,-21/13的前20项之和
时间: 2023-05-28 10:01:06 浏览: 81
C#求1-1/2+1/3-1/4+…….-1/100的值
numerator = [2, -3]
denominator = [1, 2]
for i in range(18):
new_numerator = -numerator[-1] - 2 * denominator[-1]
new_denominator = numerator[-1]
numerator.append(new_numerator)
denominator.append(new_denominator)
result = sum([numerator[i] / denominator[i] for i in range(20)])
print(result) # 输出结果为:-3.3955001588272854
阅读全文