Simple Pi calculator

by arhuman (Vicar)
 on Mar 09, 2001 at 13:51 UTC ( #63215=note: print w/replies, xml ) Need Help??

Sorry but I couldn't resist.
```#!/usr/bin/perl

\$|=1;\$N=shift||100;\$M=int(3.3*\$N)
;\$t[0]=2;\$s[0]=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 ;-)

Replies are listed 'Best First'.
Re: Simple Pi calculator
by Anonymous Monk on Jan 26, 2005 at 02:15 UTC
you nead an acule pi calkulater

Create A New User
Node Status?
node history
Node Type: note [id://63215]
help
Chatterbox?
 GotToBTru thinks erix & MySql ... talk about a database trigger [GotToBTru]: wow .. outta votes and it's not even lunchtime yet [stevieb]: berrybrew has been updated with the new perl checksums. Now I'm going to implement dynamic loading of perls thanks to their new JSON releases file

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2017-03-30 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (360 votes). Check out past polls.