Python 作业帮助:在方程中使用一系列阶乘有史以来的第一篇文章!

问题描述

我需要一些 Python 作业的帮助。 我想在 e= 1+((1/1!)+(1/2!)+(1/3!)+(1/4!)+(1/5!)+( 1/6!)+(1/7!)+(1/8!)+(1/9!)+(1/10!))

我们已经超越了范围,我现在知道找到阶乘的两种方法,但是在将所有这些结合起来时遇到了麻烦,我正在尝试测试沿途的每一步,但无法克服这个问题。而且我还得把它插入等式之后

import math
i=1

math.factorial(i)

(math.factorial(range((int(1,11)))

print (i)

a=1

for i in range(1,11):    #start off with a range from 1-10

    a += 1/i    #a represents each fraction: this is solved first

    print(a)    #this says we want to add the list of fractions together

e = 1 + a    #add 1 to all those fractions' sums

print(e)    #finally,just display what e solves to

解决方法

您应该在遍历范围时累积当前阶乘并使用它来构建结果:

def invfact(e):
    f = 1      # track factorials
    r = 1      # accumulated result
    for n in range(1,e+1):
        f *= n    # current factorial (n!)
        r += 1/f  # add to result
    return r