为什么我的Chudnovsky算法程序给出了错误的结果?

发布于 2025-02-01 15:59:48 字数 1438 浏览 3 评论 0 原文

我正在尝试在Python中编码Chudnovsky算法。但是,当我运行代码时,它给了我很少的数字(-5.051212624421025E-55),这不是PI。我在中学时,我不认识有人可以帮助我。我在做什么错?

这是Chudnovsky公式的链接: https://levelup.gitconnected.com/generating-the-phalue-the-value-of-pi-pi-pi-pi-pi-pi-a-nonning-noming-number-forn-number-findecimals-decimals-decimals-place-place-place-plass-place-in-python-e93986bbbbbbbb474d

这是我的代码:

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)

I am trying to code the Chudnovsky algorithm in python. However, when I run my code, it gives me a very small number (-5.051212624421025e-55) which is not pi. I am in middle school, and I don't know anybody that can help me. What am I doing wrong?

Here is a link to the Chudnovsky formula: https://levelup.gitconnected.com/generating-the-value-of-pi-to-a-known-number-of-decimals-places-in-python-e93986bb474d

Here is my code:

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)

Here is my updated code:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-02-08 15:59:49

我看到两个错误:

  1. i (公式中的 q ),其中您当前在循环中使用 k 。在您的代码中 k i

  2. python中的启动操作员是 ** ,而不是^(它是位xor)。

I see two mistakes:

  1. 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 use k in the loop. In your code k is the upper bound for i.

  2. The exponentiation operator in Python is **, not ^ (which is bitwise XOR).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文