python中的Pi计算 [英] Pi calculation in python

查看:178
本文介绍了python中的Pi计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

n =迭代

由于某些原因,此代码需要更多迭代才能获得其他代码的更准确结果,任何人都可以解释为什么会发生这种情况吗?谢谢。

for some reason this code will need a lot more iterations for more accurate result from other codes, Can anyone explain why this is happening? thanks.

    n,s,x=1000,1,0
    for i in range(0,n,2):
            x+=s*(1/(1+i))*4
            s=-s
    print(x)


推荐答案

正如我在评论中提到的,加速这一过程的唯一方法是转换序列。这是一个非常简单的方法,与欧拉变换有关(参见 roippi的链接):总和交替序列,创建一个新的序列,由每对连续的部分和的平均值组成。例如,给定交替序列

As I mentioned in a comment, the only way to speed this is to transform the sequence. Here's a very simple way, related to the Euler transformation (see roippi's link): for the sum of an alternating sequence, create a new sequence consisting of the average of each pair of successive partial sums. For example, given the alternating sequence

a0 -a1 +a2 -a3 +a4 ...

其中所有 a s为正数,部分和的序列为:

where all the as are positive, the sequences of partial sums is:

s0=a0  s1=a0-a1  s2=a0-a1+a2  s3=a0-a1+a2-a3  s4=a0-a1+a2-a3+a4 ...

然后新的派生序列为:

(s0+s1)/2  (s1+s2)/2  (s2+s3)/2  (s3+s4)/2 ...

通常可以更快地收敛 - 并且同样的想法可以应用于此序列。也就是说,创建另一个新的序列,平均 序列的条件。这可以无限期地进行。在这里,我将再拿一个级别:

That can often converge faster - and the same idea can applied to this sequence. That is, create yet another new sequence averaging the terms of that sequence. This can be carried on indefinitely. Here I'll take it one more level:

from math import pi

def leibniz():
    from itertools import count
    s, x = 1.0, 0.0
    for i in count(1, 2):
        x += 4.0*s/i
        s = -s
        yield x

def avg(seq):
    a = next(seq)
    while True:
        b = next(seq)
        yield (a + b) / 2.0
        a = b

base = leibniz()
d1 = avg(base)
d2 = avg(d1)
d3 = avg(d2)

for i in range(20):
    x = next(d3)
    print("{:.6f} {:8.4%}".format(x, (x - pi)/pi))

输出:

3.161905  0.6466%
3.136508 -0.1619%
3.143434  0.0586%
3.140770 -0.0262%
3.142014  0.0134%
3.141355 -0.0076%
3.141736  0.0046%
3.141501 -0.0029%
3.141654  0.0020%
3.141550 -0.0014%
3.141623  0.0010%
3.141570 -0.0007%
3.141610  0.0005%
3.141580 -0.0004%
3.141603  0.0003%
3.141585 -0.0003%
3.141599  0.0002%
3.141587 -0.0002%
3.141597  0.0001%
3.141589 -0.0001%

因此,在仅仅20个学期之后,我们已经获得了大约6位有效数字。 Leibniz基本序列仍然大约2位正确:

So after just 20 terms, we've already got pi to about 6 significant digits. The base Leibniz sequence is still at about 2 digits correct:

>>> next(base)
3.099944032373808

这是一个巨大的进步。这里的一个关键点是基础莱布尼兹序列的部分和给出了在太大和太小之间交替的近似值。这就是为什么平均它们更接近事实。相同的(在太大和太小之间交替)对于派生序列也是如此,因此平均它们的术语也会有所帮助。

That's an enormous improvement. A key point here is that the partial sums of the base Leibniz sequence give approximations that alternate between "too big" and "too small". That's why averaging them gets closer to the truth. The same (alternating between "too big" and "too small") is also true of the derived sequences, so averaging their terms also helps.

这都是手工波浪, 当然。严格的理由可能不是你感兴趣的东西; - )

That's all hand-wavy, of course. Rigorous justification probably isn't something you're interested in ;-)

这篇关于python中的Pi计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆