Leibniz_Pi.py

Thomas Heller thomas.heller at ion-tof.com
Fri Oct 11 21:33:21 CEST 2002


bhoel at web.de (Berthold =?iso-8859-15?q?H=F6llmann?=) writes:

> Try Numeric for improvements:
> 
> On my PentiumII 350MHz, GNU/Linux, Kernel 2.4.18, SuSE 8.0
> (calculate_pi2 is Numeric):
> 
> Approximate  Pi  via Leibniz Sequence 
> calculate_pi1:  4.35253095627 s
> calculate_pi2:  1.28464400768 s
> Terms :  1000000
>    Pi1:  3.14159165359
>    Pi2:  3.14159165359
>    Pi :  3.14159265359
> 
> My modified program follows.
> 
> Greetings
> Berthold

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



More information about the Python-list mailing list