Category: | text processing |
Author/Contact Info | Pete_I |
Description: | calculates the digits of pi to $ARGV[0] accurately |
#!/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)
####
#edited to make more efficient use of memory
use strict;
use warnings;
use Math::BigFloat;
my $DIGS = $ARGV[0] || 50;
Math::BigFloat->div_scale($DIGS+10);
my $pi = Math::BigFloat->new();
for(0 .. $DIGS) {
$pi->badd( get_digit($_) );
}
print "\n", $pi->round($DIGS) . "\n";
sub get_digit {
my $k = Math::BigFloat->new(shift);
( 1 / 16 ** $k ) *
(
(4 / (8 * $k + 1))-
(2 / (8 * $k + 4))-
(1 / (8 * $k + 5))-
(1 / (8 * $k + 6))
)
}
__DATA__
digits of pi to compare accuracy
3.14159265358979323846264338327950288419716939937510582097494459230781
+6406286208998628034825342117067982148086513282
|
Back to
Code Catacombs