为什么我的Chudnovsky算法程序给出了错误的结果?
我正在尝试在Python中编码Chudnovsky算法。但是,当我运行代码时,它给了我很少的数字(-5.051212624421025E-55),这不是PI。我在中学时,我不认识有人可以帮助我。我在做什么错?
这是我的代码:
import math
def fact(exi):
memory = exi
for i in range(1, exi):
memory *= i
return memory
k = 10
s = 0
for i in range(0, k):
a = -1^k
b = fact(6*k)
c = (545140134*k) + 13591409
d = fact(3*k)
e = (fact(k))^3
f = (3 * k) + 3/2
g = math.pow(640320, f)
numerator = (a*b*c)
denominator = (d*e*f)
s += (numerator / denominator)
s *= 12
print(1 / s)
这是我的更新代码:
import math
def fact(exi):
memory = exi
for i in range(1, exi):
memory *= i
return memory
k = 17
s = 0
for i in range(1, k):
a = (-1)**i
b = fact(6*i)
c = (545140134*i) + 13591409
d = fact(3*i)
e = (fact(i))**3
f = (3 * i) + 3/2
g = math.pow(640320, f)
num = (a*b*c)
den = (d*e*g)
s += (num / den)
s *= 12
print(1 / s)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到两个错误:
与 i (公式中的 q ),其中您当前在循环中使用
k
。在您的代码中k
是i
。python中的启动操作员是
**
,而不是^
(它是位xor)。I see two mistakes:
When comparing with the formula shown on Wikipedia, it looks like you should use the iteration variable
i
(named q in the formula) where you currently usek
in the loop. In your codek
is the upper bound fori
.The exponentiation operator in Python is
**
, not^
(which is bitwise XOR).