如何解决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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。