Calculating Pi in python

n = iteration

for some reason, this code will require much more iterations to get a 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)
+1
source share
2 answers

As I mentioned in the commentary, the only way to speed it up is to convert the sequence. Here's a very simple way related to the Euler transform (see roippi link ): for the sum of the alternating sequence, create a new sequence consisting of the average value for each pair of consecutive partial sums. For example, given a variable sequence

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

a , :

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

- . , , . . :

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 , pi 6 . - 2 :

>>> next(base)
3.099944032373808

. , , " " " ". . ( " " " " ) , .

, . , , , ; -)

+2

, Leibniz series , , () .

+2

All Articles