python中求pi_python中的Pi计算

n=iterations

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)

解决方案

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 ...

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 ...

and then the new derived sequence is:

(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))

Output:

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%

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 ;-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值