#!/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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|