http://qs321.pair.com?node_id=63162

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

This node falls below the community's threshold of quality. You may see it by logging in.

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[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
    (Gregory,Machin,Ramanadjuan...)

    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}.)

      -- Randal L. Schwartz, Perl hacker

        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?