 good chemistry is complicated,and a little bit messy -LW PerlMonks

Calculating Pi

 on Mar 09, 2001 at 07:38 UTC Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a program that calculates pi to a very large number of decimal places. Can someone help me with the code? my email is Kalluminati2001@yahoo.com

Replies are listed 'Best First'.
Simple Pi calculator
by arhuman (Vicar) on Mar 09, 2001 at 13:51 UTC

Sorry but I couldn't resist.
#!/usr/bin/perl \$|=1;\$N=shift||100;\$M=int(3.3*\$N) ;\$t=2;\$s=2;sub z{\$a=\$r=00}; for( \$k=1 ;\$k<\$M ;\$k++){\$a=\$r=0; };for( \$k=01; \$k<\$M;\$k++){&z; for(\$i =\$N;\$i >=0;\$i--){\$a=\$t [\$i]*( \$k)+\$r ;\$t[\$i]=int(\$a% 10);\${ r}=int (\$a/10);}\$K=(\$k <<1)+1 ;&{z}; map{\$a=\$t[\$_]+( 10)*(( \$r));; \${t}[\$_]=int(\$a /(\$K)) ;\${r}= int((\$a)%(\$K))} (0..\$N );if(( \$r>=(int((\$K)/2 )))){; ;;\${t} [\$N]++}while(\$t [\$N]>9 ){\${t} [\$N]-=10;;\$t[\$N -1]++} &z(); for(\$i=\$N;\$i>=0 ;\$i--){ \$a= (\$t[\$i]+\$s[\$i]+ \$r);\${s} [\$i]=int(\$a%10);\$r=int(\$a/10)}if( \$k>\$x+3){\${x}=\$k;;print"\$s[\$y++]" ;};}for(\$y..\${N}){print"\$s[\$_]";}
You can pass the desired number of digits as parameter on the commandline.

Of course the method used was choosed more for its simplicity than for its speed.
(But this code could be optimized several ways, by adding 'use integer;' for example...)

For REAL program you should use Math::GMP or Math::Float and real formulae

Any search on the net would give you a lot of start point, you might try this one.

"Trying to be a SMART lamer" (thanx to Merlyn ;-)
you nead an acule pi calkulater
Etiquette
by Elgon (Curate) on Mar 09, 2001 at 07:52 UTC
Kalluminati,

Before merlyn screams homework ;-) I'd just like to point out a few things.

We are all willing to help when we can but in order to help we need to see the code: Post it! You will not only get a better quality of help, you will also get XP because we like code here especially if it does something in a neat and innovative way.

The other reason is that we are not here to do your work for you: If you have tried to crack a problem (although in this case there are several well documented ways of doing it if I remember correctly) before asking for help, this is fine, but just expecting us to do the work for you is false laziness and therefore a BAD THING!

Go and have a look around the Perlmonks FAQ, monastery guide and formatting tips as this will help you a lot.

Cheers, Elgon

Just for calibration, I wasn't even thinking homework on this. I'd never assign this as homework. (That's my personal test {grin}.)
Really? You are mellowing! The 'weekly dates' thread has clearly had an effect on your life ;-)

I don't know if I would set it as homework either but it is just the sort of thing my 'programming' (matlab) teacher would set, although he also teaches maths here so that isn't too surprising. It's quite fun be 'taught' how to program by someone who has only ever done any in FORTRAN and C to model chemical systems (Plus some he did for the French military during his national service).

Hey ho! Elgon

Re: Calculating Pi
by Adam (Vicar) on Mar 09, 2001 at 07:48 UTC
Both of which are limited by Perl's floating point limitations. Perhaps Math::BigFloat will be helpful?
Re: Calculating Pi
by I0 (Priest) on Mar 13, 2001 at 02:53 UTC
Re: Calculating Pi
by I0 (Priest) on Mar 09, 2001 at 09:20 UTC
What method are you using?

Create A New User
Node Status?
node history
Node Type: perlquestion [id://63162]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2019-10-17 23:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2019 the site I miss most is:

Results (44 votes). Check out past polls.

Notices?