当前位置: 代码迷 >> 综合 >> 第2章-11 求平方与倒数序列的部分和 (15分)
  详细解决方案

第2章-11 求平方与倒数序列的部分和 (15分)

热度:30   发布时间:2024-02-09 14:04:16.0

本题要求对两个正整数m和n(m≤n)编写程序,计算序列和m?2??+1/m+(m+1)?2??+1/(m+1)+?+n?2??+1/n。

输入格式:

输入在一行中给出两个正整数m和n(m≤n),其间以空格分开。

输出格式:

在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后六位。题目保证计算结果不超过双精度范围。

输入样例:

5 10

输出样例:

sum = 355.845635
m, n = [int(x) for x in input().split()]
pow_list = [x*x for x in range(m, n+1)]
frac_list = [1/x for x in range(m, n+1)]  #分数 fraction
print("sum = {:.6f}".format(sum(pow_list + frac_list)))

 

  相关解决方案