--- Sven Bentlage <[EMAIL PROTECTED]> wrote:
> Hi everyone!
> 
> I am trying to write a program calculating pi.
> The formula I would like to use is
>               pi = 4x( (1/1) - (1/3) + (1/5) - (1/7) + (1/9) - (1/11) .... )
> or
>               $pi = 4x ( (1/$y) - ....)

Hi Sven,

The Leibniz series is very slow, but here's one implementation:

  #!/usr/bin/perl -w
  #Compute pi. Based on Leibniz's algorithm
  #       pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11...
  use strict;

  my $pi = 4;
  my $next_digits = get_leibniz();

  for ( 1 .. 100000 ) {
      my ( $subtract, $add ) =  $next_digits->();
      $pi = $pi - $subtract + $add;
  }

  print $pi;

  sub get_leibniz {
      my $index = 1;
      my $sub = sub {
          $index += 2;
          my $first = 4/$index;
          $index += 2;
          my $second = 4/$index;
          return ($first, $second);
      };
      return $sub;
  }

If you want to see some "fun" implmentations, check out this thread on Perlmonks: 
http://www.perlmonks.org/index.pl?node_id=159377.  One of my favorites:

  sub _{$---&&4/$----&_}print- _$-=1e5

Cheers,
Ovid

=====
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A

__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to