[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] algorithm to calculate pi



From: "Perl lover" <perl@antionline.org>
> looking for a short one in Perl. saw one in C:
> 
> long a=10000, b,c=2800,d,e,f[2801],g;
> 
> int main(int argc, char* argv[]) {
>      for (;b-c;)f[b++]=a/5;
>      for (;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
>      for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
>      return 0;
> }
> 
> gets pi's first 280 digits..

I suppose,

#!/usr/bin/perl -w
use strict;
use integer;

my ($a, $b, $c, $d, $e, $g, @f) = (10000, 0, 2800, 0, 0, 0, (0)x2801);

for (;$b-$c;) {
        $f[$b++]=$a/5;
}

for (; $d=0, $g=$c*2; $c-=14, printf("%.4d",$e+$d/$a), $e=$d%$a) {
        for($b=$c; $d+=$f[$b]*$a,$f[$b]=$d%--$g,$d/=$g--,--$b; $d*=$b) {
        }
}

exit 0;


would be cheating? :-)

Paul, deliberately making no optimizations or idiomizing.


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe