pi generation
Pete_I
created: 2006-01-28 22:09:54
#!/usr/bin/perl
###
#generate $ARGV[0] digits of pi.
#the algorithm is here:
#http://crd.lbl.gov/~dhbailey/
####
#written by Pete_I with help from BillN1VUX(from freenode)
####

use strict;
use warnings;
use Math::BigFloat;

my $DIGS = $ARGV[0] || 50;
Math::BigFloat->div_scale($DIGS+10);

my $pi = Math::BigFloat->new();
my @digits = map { get_digit( $_ ) } 0 .. $DIGS;
for my $d (reverse @digits) {
 $pi=$pi->bdiv(16);
 $pi->badd($d);
}
print "\n", $pi->round($DIGS) . "\n";

sub get_digit {
 my $k = Math::BigFloat->new(shift);
 (
  (4 / (8 * $k + 1))-
  (2 / (8 * $k + 4))-
  (1 / (8 * $k + 5))-
  (1 / (8 * $k + 6))
 )
}

__DATA__
digits of pi to compare accuracy
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282
Re: pi generation
created: 2006-02-01 23:18:48
Thanks Pete, that was a fun little project. I was happy to help since no one else on IRC seemed to notice your question and it interested me. It was a great experience for my first time on IRC. (I'd ignored IRC for longer than I ignored Perl 1..4. I'm not sure I have time to be a regular everywhere, but it was fun, so I'll be back.)

My notes on how we fixed this program, and how it connects to other things, are on my journal http://use.perl.org/~n1vux/journal/28505.

In order to link a comment on your posting to my journal entry, I decided it was time to do a properly signed Monk posting -- so I had to re-create my twice-zombied ID and actually log in, so my first IRC session begats my first monkish scriptorium.

The whole Perl Monks theme amuses me in a nostalgic sort of way, since in the seventies, my main D&D characters ran a religious order for fun and profit.

So thanks for the opportunity!

perlmonks.org content © perlmonks.org and Bill_N1VUX, Pete_I

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03