Leibniz_Pi.py

Berthold Höllmann bhoel at web.de
Fri Oct 11 21:49:41 CEST 2002


Thomas Heller <thomas.heller at ion-tof.com> writes:

> bhoel at web.de (Berthold =?iso-8859-15?q?H=F6llmann?=) writes:
> 
...
> 
> This one is easier to understand, IMO, and nearly as fast as the
> Numeric version:
> 
> def calculate_pi(nTerms):
>     sum = 0.0
>     limit = nTerms*2-1
>     for i in range(1, limit, 4):
>         sum = sum + 1.0/i - 1.0/(i+2)
>     return sum * 4.0
> 
> Thomas

<pedantic>
But it delivers "wrong" results for odd number of terms:

calculate_pi1:  9.00030136108e-05 s
calculate_pi2:  0.000662922859192 s
calculate_pi3:  0.000118970870972 s
Terms :  3
   Pi1:  3.46666666667
   Pi2:  3.46666666667
   Pi3:  2.66666666667

and is (on my machine) still a lot slower when it comes to large
number of terms:

calculate_pi1:  4.48513090611 s
calculate_pi2:  1.29995203018 s
calculate_pi3:  3.30695199966 s
Terms :  1000000
   Pi1:  3.14159165359
   Pi2:  3.14159165359
   Pi3:  3.14159165359

</pedantic>

Greetings
Berthold
-- 
bhoel at web.de / http://starship.python.net/crew/bhoel/
        It is unlawful to use this email address for unsolicited ads
        (USC Title 47 Sec.227). I will assess a US$500 charge for
        reviewing and deleting each unsolicited ad.



More information about the Python-list mailing list