哪里可以下载很多位数的pi?[关闭]


11

在哪里可以找到大量的pi数字?我已经用PiFast计算了31.4亿(在葡萄酒下效果很好)。

我不在乎下载速度慢。


2
您是否出于某些远程实际目的还是仅出于...的需要?我看不出重点,所以我很好奇。
Rook

2
@Idigas:你没做过pi吗?
Nosredna

很快,我可以找到用于计算pi的算法,我将写一些东西来计算您想要的数量...
RCIX

2
继续尝试尝试接受新的答案。最初接受的答案只有一个链接不再存在,因此已被删除。如果对主持人有任何疑问,请标记问题。
Troggy

Answers:


9

我知道您说您不在乎,但是我严重怀疑您的CPU可以比网卡下载它们更快地计算它们。

给定最后一位数字和用于生成它的计算器的当前状态,可以在恒定时间内找到下一位数字。它不会像找到下一个素数那样变得越来越困难。


是的,但是要花费大量的cpu时间,我宁愿花一些带宽而不是全部的cpu时间。
bgw

@Joel:顺便说一句,您能显示一个指向该算法的指针吗?(是的,我知道那更像是SO内容,但是既然我们在这里...)
R. Martinho Fernandes


数学超出了我的范围,但在Wikipedia上却读得很少,据说其中一个系列“每学期提供14位数字”。
Joel Coehoorn

抱歉,错误链接:numbers.computation.free.fr/Constants/PiProgram/algo.html,它以帧为单位
bgw


4

在Ubuntu上,您可以 sudo apt-get install pi

接着:

$ pi 100 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

给定要计算的位数,它可以计算任意精度。


0

如果您想使用Python进行计算,这是一种非常快速的方法(使用Python和gmpy2库):

http://www.craig-wood.com/nick/articles/pi-chudnovsky/

这是一个带有小修正的代码:

"""
Python3 program to calculate Pi using python long integers, binary
splitting and the Chudnovsky algorithm

See: http://www.craig-wood.com/nick/articles/pi-chudnovsky/ for more
info

Nick Craig-Wood <nick@craig-wood.com>
"""

import math
from gmpy2 import mpz
from time import time
import gmpy2

def pi_chudnovsky_bs(digits):
    """
    Compute int(pi * 10**digits)

    This is done using Chudnovsky's series with binary splitting
    """
    C = 640320
    C3_OVER_24 = C**3 // 24
    def bs(a, b):
        """
        Computes the terms for binary splitting the Chudnovsky infinite series

        a(a) = +/- (13591409 + 545140134*a)
        p(a) = (6*a-5)*(2*a-1)*(6*a-1)
        b(a) = 1
        q(a) = a*a*a*C3_OVER_24

        returns P(a,b), Q(a,b) and T(a,b)
        """
        if b - a == 1:
            # Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
            if a == 0:
                Pab = Qab = mpz(1)
            else:
                Pab = mpz((6*a-5)*(2*a-1)*(6*a-1))
                Qab = mpz(a*a*a*C3_OVER_24)
            Tab = Pab * (13591409 + 545140134*a) # a(a) * p(a)
            if a & 1:
                Tab = -Tab
        else:
            # Recursively compute P(a,b), Q(a,b) and T(a,b)
            # m is the midpoint of a and b
            m = (a + b) // 2
            # Recursively calculate P(a,m), Q(a,m) and T(a,m)
            Pam, Qam, Tam = bs(a, m)
            # Recursively calculate P(m,b), Q(m,b) and T(m,b)
            Pmb, Qmb, Tmb = bs(m, b)
            # Now combine
            Pab = Pam * Pmb
            Qab = Qam * Qmb
            Tab = Qmb * Tam + Pam * Tmb
        return Pab, Qab, Tab
    # how many terms to compute
    DIGITS_PER_TERM = math.log10(C3_OVER_24/6/2/6)
    N = int(digits/DIGITS_PER_TERM + 1)
    # Calclate P(0,N) and Q(0,N)
    P, Q, T = bs(0, N)
    one_squared = mpz(10)**(2*digits)
    #sqrtC = (10005*one_squared).sqrt()
    sqrtC = gmpy2.isqrt(10005*one_squared)
    return (Q*426880*sqrtC) // T

# The last 5 digits or pi for various numbers of digits
check_digits = {
        100 : 70679,
       1000 :  1989,
      10000 : 75678,
     100000 : 24646,
    1000000 : 58151,
   10000000 : 55897,
}

if __name__ == "__main__":
    digits = 100
    pi = pi_chudnovsky_bs(digits)
    print(pi)
    #raise SystemExit
    for log10_digits in range(1,9):
        digits = 10**log10_digits
        start =time()
        pi = pi_chudnovsky_bs(digits)
        print("chudnovsky_gmpy_mpz_bs: digits",digits,"time",time()-start)
        if digits in check_digits:
            last_five_digits = pi % 100000
            if check_digits[digits] == last_five_digits:
                print("Last 5 digits %05d OK" % last_five_digits)
                open("%s_pi.txt" % log10_digits, "w").write(str(pi))
            else:
                print("Last 5 digits %05d wrong should be %05d" % (last_five_digits, check_digits[digits]))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.