试图使用python中的chudnovsky算法准确获取100位PI数字

发布于 2025-02-02 03:35:34 字数 1324 浏览 3 评论 0 原文

我正在尝试使用Python和此Wikipedia页面来教自己Chudnovsky的算法:

,我专注于“高性能迭代实现,[]可以简化为:

在Wiki上 “ rel =“ nofollow noreferrer”> “在此处输入图像描述”

我试图在远面的方程式上进行编码正确的是使用Sigma符号。我对Python很熟悉,但在数学方面并不那么出色。我为自己设定的目标是看看我是否可以准确打印出至少100位PI数字。

公式中有5组括号,因此我尝试对5个不同组件中的每个组件进行编码。我还写了一个可以执行阶乘的函数,因为阶乘在5个组件/括号中的3个中使用。

这是我的23行工作代码,有人可以帮助我理解为什么它不能准确地使用100位数字吗?它准确地转到第28位:3.1415926535897932384626433832。然后,对于第29位,它说8,但应该是7 ...

import math
from decimal import *

def factorial(n):
    if n == 0:
        return 1
    memory = n
    
    for i in range(1, n):
        memory *= i
    
    return memory

iterations = 500
_sum = 0

#here's the Sigma part
for q in range(0, iterations):
    a = factorial(6*q)
    b = (545140134*q) + 13591409
       
    c = factorial(3*q)
    d = (factorial(q))**3
    e = (-262537412640768000)**q
    
    numerator = (a*b)
    denominator = (c*d*e)

    _sum += Decimal(numerator / denominator)

#ensures that you get 100 digits for pi
getcontext().prec = 100

sq = Decimal(10005).sqrt()
overPI = Decimal(426880 * sq)

pi = (overPI) * (Decimal(1 / _sum))
print("Pi is", pi)

谢谢您提供的任何帮助。

I am trying to teach myself Chudnovsky's algorithm using Python and this wikipedia page:

https://en.wikipedia.org/wiki/Chudnovsky_algorithm

On the wiki, I am focused on the "high performance iterative implementation, [that] can be simplified to":

enter image description here

I tried to code up the equation on the far right that is using the Sigma symbol. I am familiar with Python but am not that great at math. The goal I set for myself is to see if I can accurately print out at least 100 digits of pi.

There are 5 sets of parentheses in the formula so I tried to code up each of the 5 different components. I also wrote a function that does factorials because factorials are used in 3 of the 5 components/parentheses.

Here's my 23 lines of working code, can someone please help me understand why it does not ACCURATELY go to 100 digits? It accurately goes to the 28th digit: 3.1415926535897932384626433832. Then for the 29th digit it says 8 but it should be 7...

import math
from decimal import *

def factorial(n):
    if n == 0:
        return 1
    memory = n
    
    for i in range(1, n):
        memory *= i
    
    return memory

iterations = 500
_sum = 0

#here's the Sigma part
for q in range(0, iterations):
    a = factorial(6*q)
    b = (545140134*q) + 13591409
       
    c = factorial(3*q)
    d = (factorial(q))**3
    e = (-262537412640768000)**q
    
    numerator = (a*b)
    denominator = (c*d*e)

    _sum += Decimal(numerator / denominator)

#ensures that you get 100 digits for pi
getcontext().prec = 100

sq = Decimal(10005).sqrt()
overPI = Decimal(426880 * sq)

pi = (overPI) * (Decimal(1 / _sum))
print("Pi is", pi)

Thank you for any assistance that you're able to provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小猫一只 2025-02-09 03:35:34
  • 在文件的 start 上设置小数精度,以便将其应用于所有后续操作:

     来自小数进口 *
    getContext()。prec = 100 
     
  • 在除分配或乘法之前,将操作数转换为 DECIMAL :< /p>

     #不好,浮动到小数 - &gt;丧失精度
    _sum +=十进制(分子 /分母)
    
    #更好,保留精度
    _sum +=十进制(分子) /十进制(分母) 
     

结果 - 精确到98 dp(100 sf减去舍入错误):

#     3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Pi is 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117069
  • Set the decimal precision at the start of the file so that it is applied to all subsequent operations:

    from decimal import *
    getcontext().prec = 100 
    
  • Convert either or both operands to Decimal before dividing or multiplying:

    # bad, float to decimal -> loss of precision
    _sum += Decimal(numerator / denominator)
    
    # better, precision preserved
    _sum += Decimal(numerator) / Decimal(denominator) 
    

Result - accurate to 98 d.p. (100 s.f. minus rounding error):

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