Python中的1000位圆周率

发布于 2024-12-29 04:48:59 字数 825 浏览 5 评论 0 原文

我一直在想这个问题,想不通。也许你可以帮助我。问题是我的代码无法以 Python 编码语言输出 1000 位 pi。

这是我的代码:

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    while True:
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = (10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x)
        else:
            q, r, t, k, m, x = (q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2)

digits = make_pi()
pi_list = []
my_array = []
for i in range(1000):
    my_array.append(str("hello, I'm an element in an array \n" ))
big_string = "".join(my_array)

print "here is a big string:\n %s" % big_string 

我知道可以修复此代码以使其工作,但我不确定要修复什么... print 语句说这里是一个大字符串,而 my_array.append (str("hello, im an element in an array \n)) 现在只是一个填充符。我知道所有代码是如何工作的,但就像我之前说的,我无法理解射出该代码。

I have been thinking about this issue and I can't figure it out. Perhaps you can assist me. The problem is my code isn't working to output 1000 digits of pi in the Python coding language.

Here's my code:

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    while True:
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = (10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x)
        else:
            q, r, t, k, m, x = (q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2)

digits = make_pi()
pi_list = []
my_array = []
for i in range(1000):
    my_array.append(str("hello, I'm an element in an array \n" ))
big_string = "".join(my_array)

print "here is a big string:\n %s" % big_string 

I know this code can be fixed to work, but I'm not sure what to fix... The print statement saying here is a big string and the my_array.append(str("hello, im an element in an array \n)) is just a filler for now. I know how all the code is used to work, but like I said before, I can't get it to shoot out that code.

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

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

发布评论

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

评论(11

过潦 2025-01-05 04:48:59

如果您不想实现自己的算法,可以使用mpmath

try:
    # import version included with old SymPy
    from sympy.mpmath import mp
except ImportError:
    # import newer version
    from mpmath import mp

mp.dps = 1000  # set number of digits
print(mp.pi)   # print pi to a thousand places

参考

更新:代码支持 SymPy 的旧版和新版安装(请参阅评论)。*

If you don't want to implement your own algorithm, you can use mpmath.

try:
    # import version included with old SymPy
    from sympy.mpmath import mp
except ImportError:
    # import newer version
    from mpmath import mp

mp.dps = 1000  # set number of digits
print(mp.pi)   # print pi to a thousand places

Reference

Update: Code supports older and newer installations of SymPy (see comment).*

荒岛晴空 2025-01-05 04:48:59

运行这个

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    for j in range(1000):
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2


my_array = []

for i in make_pi():
    my_array.append(str(i))

my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array)
print "here is a big string:\n %s" % big_string 

并从这里阅读有关 yield 运算符的内容:
“yield”关键字有什么作用?

答案如下:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337

Run this

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    for j in range(1000):
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2


my_array = []

for i in make_pi():
    my_array.append(str(i))

my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array)
print "here is a big string:\n %s" % big_string 

And read about yield operator from here:
What does the "yield" keyword do?

Here is the answer:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337
瑕疵 2025-01-05 04:48:59

正如评论中所述,接受的答案是不正确的。

OP的代码似乎基于从Spigot算法的实现://mail.python.org/pipermail/edu-sig/2006-July/006810.html" rel="noreferrer">此处。

要根据OP的问题修复代码(尽管我重命名了变量和函数以匹配它们在原始源中的内容),一种解决方案可能是:

#!/usr/bin/env python

DIGITS = 1000

def pi_digits(x):
    """Generate x digits of Pi."""
    q,r,t,k,n,l = 1,0,1,1,3,3
    while x >= 0:
        if 4*q+r-t < x*t:
            yield n
            x -= 1
            q,r,t,k,n,l = 10*q, 10*(r-n*t), t, k, (10*(3*q + r))/t-10*n, l
        else:
            q,r,t,k,n,l = q*k, (2*q+r)*l, t*l, k+1, (q*(7*k+2)+r*l)/(t*l), l+2

digits = [str(n) for n in list(pi_digits(DIGITS))]
print("%s.%s\n" % (digits.pop(0), "".join(digits)))

另外,这里有一个更快的*实现,显然也基于Spigot的算法:

#!/usr/bin/env python

DIGITS = 1000

def pi_digits(x):
    """Generate x digits of Pi."""
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1

digits = [str(n) for n in list(pi_digits(DIGITS))]
print("%s.%s\n" % (digits.pop(0), "".join(digits)))

我针对这个在线 Pi 数字生成器对两者进行了几次测试。

所有功劳均归功于 Gist,作者:深度查找

* 基于对 10,000 个数字的测试,我得到了大约 7 秒,而相比之下,我得到了大约 1 秒。

The accepted answer is incorrect, as noted in comments.

The OP's code appears to be based on an implementation of Spigot's algorithm copied from here.

To fix the code per the OP's question (although I renamed the variables and functions to match what they were in the original source), one solution might be:

#!/usr/bin/env python

DIGITS = 1000

def pi_digits(x):
    """Generate x digits of Pi."""
    q,r,t,k,n,l = 1,0,1,1,3,3
    while x >= 0:
        if 4*q+r-t < x*t:
            yield n
            x -= 1
            q,r,t,k,n,l = 10*q, 10*(r-n*t), t, k, (10*(3*q + r))/t-10*n, l
        else:
            q,r,t,k,n,l = q*k, (2*q+r)*l, t*l, k+1, (q*(7*k+2)+r*l)/(t*l), l+2

digits = [str(n) for n in list(pi_digits(DIGITS))]
print("%s.%s\n" % (digits.pop(0), "".join(digits)))

Also, here is a much faster* implementation, also apparently based on Spigot's algorithm:

#!/usr/bin/env python

DIGITS = 1000

def pi_digits(x):
    """Generate x digits of Pi."""
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1

digits = [str(n) for n in list(pi_digits(DIGITS))]
print("%s.%s\n" % (digits.pop(0), "".join(digits)))

I tested both a few times against this online Pi digit generator.

All credit to this Gist by deeplook.

* Based on testing 10,000 digits, where I got about 7 seconds compared to about 1 second.

趁微风不噪 2025-01-05 04:48:59

对于最多 100 万位 pi,请使用 math_pi (注意:我是模块)

使用 pip 安装:

pip install math-pi

在 Python 中:

>>> import math_pi
>>> print(math_pi.pi(b=1000))
3.1415926535...

For up to 1 million digits of pi use math_pi (note: I am the author of the module)

Install with pip:

pip install math-pi

In Python:

>>> import math_pi
>>> print(math_pi.pi(b=1000))
3.1415926535...
彼岸花ソ最美的依靠 2025-01-05 04:48:59

来自 Fabrice Bellard 网站:Pi 计算算法。很抱歉如此简单的实现。 1000 已经足够快了(对我来说 0.1 秒),但 10000 就没那么快了 - 71 秒:-(

import time
from decimal import Decimal, getcontext

def compute(n):
    getcontext().prec = n
    res = Decimal(0)
    for i in range(n):
        a = Decimal(1)/(16**i)
        b = Decimal(4)/(8*i+1)
        c = Decimal(2)/(8*i+4)
        d = Decimal(1)/(8*i+5)
        e = Decimal(1)/(8*i+6)
        r = a*(b-c-d-e)
        res += r
    return res

if __name__ == "__main__":
    t1 = time.time()
    res = compute(1000)
    dt = time.time()-t1
    print(res)
    print(dt)

From Fabrice Bellard site: Pi Computation algorithm. Sorry for such a straightforward implementation. 1000 is fast enough (0.1s for me), but 10000 isn't such fast - 71s :-(

import time
from decimal import Decimal, getcontext

def compute(n):
    getcontext().prec = n
    res = Decimal(0)
    for i in range(n):
        a = Decimal(1)/(16**i)
        b = Decimal(4)/(8*i+1)
        c = Decimal(2)/(8*i+4)
        d = Decimal(1)/(8*i+5)
        e = Decimal(1)/(8*i+6)
        r = a*(b-c-d-e)
        res += r
    return res

if __name__ == "__main__":
    t1 = time.time()
    res = compute(1000)
    dt = time.time()-t1
    print(res)
    print(dt)
两人的回忆 2025-01-05 04:48:59

5-6年前我用下面的公式解决了这个问题。

类机器公式

维基百科:https://en.wikipedia.org /wiki/Machin-like_formula

数学公式

对代码质量表示歉意。变量名可能毫无意义。

#-*- coding: utf-8 -*-

# Author:    Fatih Mert Doğancan
# Date:      02.12.2014

def arccot(x, u):
    sum = ussu = u // x
    n = 3
    sign = -1
    while 1:
        ussu = ussu // (x*x)
        term = ussu // n
        if not term:
            break
        sum += sign * term
        sign = -sign
        n += 2
    return sum

def pi(basamak):
    u = 10**(basamak+10)
    pi = 4 * (4*arccot(5,u) - arccot(239,u))
    return pi // 10**10

if __name__ == "__main__":
    print pi(1000) # 1000 

I was solved with bellow formula 5-6 years ago.

Machin-like formula

Wikipedia: https://en.wikipedia.org/wiki/Machin-like_formula

Math formula

Sorry for the code quality. Variable names can be meaningless.

#-*- coding: utf-8 -*-

# Author:    Fatih Mert Doğancan
# Date:      02.12.2014

def arccot(x, u):
    sum = ussu = u // x
    n = 3
    sign = -1
    while 1:
        ussu = ussu // (x*x)
        term = ussu // n
        if not term:
            break
        sum += sign * term
        sign = -sign
        n += 2
    return sum

def pi(basamak):
    u = 10**(basamak+10)
    pi = 4 * (4*arccot(5,u) - arccot(239,u))
    return pi // 10**10

if __name__ == "__main__":
    print pi(1000) # 1000 
冷弦 2025-01-05 04:48:59

我不熟悉你的算法。它是BBP的实现吗?

无论如何,您的 make_pi 都是一个生成器。尝试在 for 循环中使用它:

for digit in make_pi():
    print digit

请注意,此循环是无限的:make_pi() 永远不会抛出 StopIteration

I'm not familiar with your algorithm. Is it an implementation of BBP?

In any case, your make_pi is a generator. Try using it in a for loop:

for digit in make_pi():
    print digit

Note that this loop is infinite: make_pi() never throws StopIteration

好听的两个字的网名 2025-01-05 04:48:59

在这里你可以检查你的程序是否输出正确的1000位数字:
http://spoj.com/CONSTANT

当然你也可以使用 diff 或 tc 但你必须从某处复制这 1000 个数字,然后提交程序并检查分数是否大于 999。

您可以尝试在那里打印更多数字,从而获得更多分数。也许你会喜欢它。

Here you can check whether your program outputs correct 1000 digits:
http://spoj.com/CONSTANT

Of course you can use diff or tc as well but you'd have to copy these 1000 digits from somewhere and there you just submit your program and check whether the score is bigger than 999.

You can try to print even more digits there and thus get more points. Perhaps you'd enjoy it.

撑一把青伞 2025-01-05 04:48:59

这是你想要的吗?

i = 0;
pi_str = ""
for x in make_pi():
    pi_str += str(x)
    i += 1
    if i == 1001:
        break

print "pi= %s.%s" % (pi_str[0],pi_str[1:])

Does this do what you want?

i = 0;
pi_str = ""
for x in make_pi():
    pi_str += str(x)
    i += 1
    if i == 1001:
        break

print "pi= %s.%s" % (pi_str[0],pi_str[1:])
青柠芒果 2025-01-05 04:48:59

这是我在这里找到的另一种方式 --> Python pi 计算? 基于 Chudnovsky brothers 生成 Pi 的公式来近似 python,我已经对其进行了稍微修改我的程序。

def pifunction():
    numberofdigits = int(input("please enter the number of digits of pi that you want to generate"))
    getcontext().prec = numberofdigits

def calc(n):
    t = Decimal(0)
    pi = Decimal(0)
    deno = Decimal(0)
    k = 0
    for k in range(n):
        t = (Decimal(-1)**k)*(math.factorial(Decimal(6)*k))*(13591409+545140134*k)
        deno = math.factorial(3*k)*(math.factorial(k)**Decimal(3))*(640320**(3*k))
        pi += Decimal(t)/Decimal(deno)
    pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5))
    pi = 1/pi
    return str(pi)
print(calc(1))

我希望这会有所帮助,因为您可以生成您想要生成的任意位数的 pi。

Here is a different way I found here --> Python pi calculation? to approximate python based on the Chudnovsky brothers formula for generating Pi which I have sightly modified for my program.

def pifunction():
    numberofdigits = int(input("please enter the number of digits of pi that you want to generate"))
    getcontext().prec = numberofdigits

def calc(n):
    t = Decimal(0)
    pi = Decimal(0)
    deno = Decimal(0)
    k = 0
    for k in range(n):
        t = (Decimal(-1)**k)*(math.factorial(Decimal(6)*k))*(13591409+545140134*k)
        deno = math.factorial(3*k)*(math.factorial(k)**Decimal(3))*(640320**(3*k))
        pi += Decimal(t)/Decimal(deno)
    pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5))
    pi = 1/pi
    return str(pi)
print(calc(1))

I hope this helps as you can generate any number of digits of pi that you wish to generate.

权谋诡计 2025-01-05 04:48:59

wallis公式可以得到3.141592661439964,但需要更有效的方法来解决这个问题。

https://www.youtube.com/watch?v=EZSiQv_G9HM

现在我的代码

x, y, summing = 2, 3, 4

for count in range (0,100000000):
    summing *= (x/y)
    x += 2
    summing *= (x/y)
    y += 2

print (summing)

wallis formula can get to 3.141592661439964 but a more efficient way is needed to solve this problem.

https://www.youtube.com/watch?v=EZSiQv_G9HM

and now my code

x, y, summing = 2, 3, 4

for count in range (0,100000000):
    summing *= (x/y)
    x += 2
    summing *= (x/y)
    y += 2

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