仿照求$ \sum_{i=1}^mi + \sum_{i=1}^ni + \sum_{i=1}^ki$的完整代码,写程序,可求m!+n!+k!


In [8]:
def compute_mult(end):
    i=0
    total_n=1
    
    while i< end:
        i=i+1
        total_n=total_n*i
    return total_n

n = int(input('请输入第1个整数,以回车结束。'))
m = int(input('请输入第2个整数,以回车结束。'))
k = int(input('请输入第3个整数,以回车结束。'))

print('最终的和是:', compute_mult(m) + compute_mult(n) + compute_mult(k))


请输入第1个整数,以回车结束。5
请输入第2个整数,以回车结束。6
请输入第3个整数,以回车结束。7
最终的和是: 5880

写函数可返回1 - 1/3 + 1/5 - 1/7...的前n项的和。在主程序中,分别令n=1000及100000,打印4倍该函数的和


In [20]:
def compute_sum(end):
    i=0
    total=0
    while i<end:
        i=i+1
        total=total+(-1)**(i-1)*1/(i*2-1)
    return total


n=1000
m=100000
print(4*compute_sum(n),4*compute_sum(m))


3.140592653839794 3.1415826535897198