learnpython.org - an online interactive Python tutorial

harrismh777 harrismh777 at charter.net
Mon Apr 25 08:14:24 CEST 2011


Dave Angel wrote:
>> time echo "scale = 1010; 16 * a(1/5) - 4 * a(1/239)" |bc -lq
>>

> Wouldn't it be shorter to say:
>
> time echo "scale = 1010;  4 * a(1)" |bc -lq

Well, you can check it out by doing the math... (its fun...)

...  you will notice that 'time' is called first, which on *nix systems 
clocks the processing, breaking out the system and user times... right?

... so try these 10,000 comparisons on your *nix system:

     time echo "scale = 10010; 16 * a(1/5) - 4 * a(1/239)" |bc -lq

     time echo "scale = 10010;  4 * a(1)" |bc -lq

(those will take a few minutes, if your processor is running 2-3Ghz...)

... then try these 100,000 runs:

     time echo "scale = 100010; 16 * a(1/5) - 4 * a(1/239)" |bc -lq

     time echo "scale = 100010;  4 * a(1)" |bc -lq

(Those will take some time, probably less than 20 - 30 minutes... )


After your time comparisons, tell me whether you want to use a(1)*4 ??

Leonard Euler came up with the formula I'm using here... and used it 
himself for paper 'n pencil arithmetic... because the arctan(n) infinite 
series converges much quicker (by orders of magnitude) for values of (n) 
< 0.  (the smaller the (n) the better)

We can make the entire function run even faster by using smp and 
splitting the  'a(1/5)' and  'a(1/239)' across two cores, having the 
arctan series' running in parallel. This is important if your 'big num' 
is going to be hundreds of thousands or millions of places. On my baby 
Beowulf cluster I have played a bit with running this on two separate 
systems and then bringing the result together in the end... fun for 
playtime... interesting to see how many digits (in how much time) can be 
achieved *without* using a super-computer....

You can also try these formula for comparison sake:

     PI  =  20 * a(1/7) + 8 * a(3/79)
or
     PI  =  8 * a(1/3) + 4 * a(1/7)
or
     PI  =  24 * a(1/8) + 8 * a(1/57) + 4 * a(1/239)



Happy Easter,  and have a slice of pie on me....   :)





kind regards,
m harris






More information about the Python-list mailing list