Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Pi/Perl/pi-1.pl
Normal file
41
Task/Pi/Perl/pi-1.pl
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
sub pistream {
|
||||
my $digits = shift;
|
||||
my(@out, @a);
|
||||
my($b, $c, $d, $e, $f, $g, $i, $d4, $d3, $d2, $d1);
|
||||
my $outi = 0;
|
||||
|
||||
$digits++;
|
||||
$b = $d = $e = $g = $i = 0;
|
||||
$f = 10000;
|
||||
$c = 14 * (int($digits/4)+2);
|
||||
@a = (20000000) x $c;
|
||||
print "3.";
|
||||
while (($b = $c -= 14) > 0 && $i < $digits) {
|
||||
$d = $e = $d % $f;
|
||||
while (--$b > 0) {
|
||||
$d = $d * $b + $a[$b];
|
||||
$g = ($b << 1) - 1;
|
||||
$a[$b] = ($d % $g) * $f;
|
||||
$d = int($d / $g);
|
||||
}
|
||||
$d4 = $e + int($d/$f);
|
||||
if ($d4 > 9999) {
|
||||
$d4 -= 10000;
|
||||
$out[$i-1]++;
|
||||
for ($b = $i-1; $out[$b] == 1; $b--) {
|
||||
$out[$b] = 0;
|
||||
$out[$b-1]++;
|
||||
}
|
||||
}
|
||||
$d3 = int($d4/10);
|
||||
$d2 = int($d3/10);
|
||||
$d1 = int($d2/10);
|
||||
$out[$i++] = $d1;
|
||||
$out[$i++] = $d2-$d1*10;
|
||||
$out[$i++] = $d3-$d2*10;
|
||||
$out[$i++] = $d4-$d3*10;
|
||||
print join "", @out[$i-15 .. $i-15+3] if $i >= 16;
|
||||
}
|
||||
# We've closed the spigot. Print the remainder without rounding.
|
||||
print join "", @out[$i-15+4 .. $digits-2], "\n";
|
||||
}
|
||||
44
Task/Pi/Perl/pi-2.pl
Normal file
44
Task/Pi/Perl/pi-2.pl
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use bigint try=>"GMP";
|
||||
sub stream {
|
||||
my ($next, $safe, $prod, $cons, $z, $x) = @_;
|
||||
$x = $x->();
|
||||
sub {
|
||||
while (1) {
|
||||
my $y = $next->($z);
|
||||
if ($safe->($z, $y)) {
|
||||
$z = $prod->($z, $y);
|
||||
return $y;
|
||||
} else {
|
||||
$z = $cons->($z, $x->());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub extr {
|
||||
use integer;
|
||||
my ($q, $r, $s, $t) = @{shift()};
|
||||
my $x = shift;
|
||||
($q * $x + $r) / ($s * $x + $t);
|
||||
}
|
||||
|
||||
sub comp {
|
||||
my ($q, $r, $s, $t) = @{shift()};
|
||||
my ($u, $v, $w, $x) = @{shift()};
|
||||
[$q * $u + $r * $w,
|
||||
$q * $v + $r * $x,
|
||||
$s * $u + $t * $w,
|
||||
$s * $v + $t * $x];
|
||||
}
|
||||
|
||||
my $pi_stream = stream
|
||||
sub { extr shift, 3 },
|
||||
sub { my ($z, $n) = @_; $n == extr $z, 4 },
|
||||
sub { my ($z, $n) = @_; comp([10, -10*$n, 0, 1], $z) },
|
||||
\&comp,
|
||||
[1, 0, 0, 1],
|
||||
sub { my $n = 0; sub { $n++; [$n, 4 * $n + 2, 0, 2 * $n + 1] } },
|
||||
;
|
||||
$|++;
|
||||
print $pi_stream->(), '.';
|
||||
print $pi_stream->() while 1;
|
||||
54
Task/Pi/Perl/pi-3.pl
Normal file
54
Task/Pi/Perl/pi-3.pl
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use bigint try=>"GMP";
|
||||
|
||||
# Pi/4 = 4 arctan 1/5 - arctan 1/239
|
||||
# expanding it with Taylor series with what's probably the dumbest method
|
||||
|
||||
my ($ds, $ns) = (1, 0);
|
||||
my ($n5, $d5) = (16 * (25 * 3 - 1), 3 * 5**3);
|
||||
my ($n2, $d2) = (4 * (239 * 239 * 3 - 1), 3 * 239**3);
|
||||
|
||||
sub next_term {
|
||||
my ($coef, $p) = @_[1, 2];
|
||||
$_[0] /= ($p - 4) * ($p - 2);
|
||||
$_[0] *= $p * ($p + 2) * $coef**4;
|
||||
}
|
||||
|
||||
my $p2 = 5;
|
||||
my $pow = 1;
|
||||
|
||||
$| = 1;
|
||||
for (my $x = 5; ; $x += 4) {
|
||||
($ns, $ds) = ($ns * $d5 + $n5 * $pow * $ds, $ds * $d5);
|
||||
|
||||
next_term($d5, 5, $x);
|
||||
$n5 = 16 * (5 * 5 * ($x + 2) - $x);
|
||||
|
||||
while ($d5 > $d2) {
|
||||
($ns, $ds) = ($ns * $d2 - $n2 * $pow * $ds, $ds * $d2);
|
||||
$n2 = 4 * (239 * 239 * ($p2 + 2) - $p2);
|
||||
next_term($d2, 239, $p2);
|
||||
$p2 += 4;
|
||||
}
|
||||
|
||||
my $ppow = 1;
|
||||
while ($pow * $n5 * 5**4 < $d5 && $pow * $n2 * $n2 * 239**4 < $d2) {
|
||||
$pow *= 10;
|
||||
$ppow *= 10;
|
||||
}
|
||||
|
||||
if ($ppow > 1) {
|
||||
$ns *= $ppow;
|
||||
#FIX? my $out = $ns->bdiv($ds); # bugged?
|
||||
my $out = $ns / $ds;
|
||||
$ns %= $ds;
|
||||
|
||||
$out = ("0" x (length($ppow) - length($out) - 1)) . $out;
|
||||
print $out;
|
||||
}
|
||||
|
||||
if ( $p2 % 20 == 1) {
|
||||
my $g = Math::BigInt::bgcd($ds, $ns);
|
||||
$ds /= $g;
|
||||
$ns /= $g;
|
||||
}
|
||||
}
|
||||
18
Task/Pi/Perl/pi-4.pl
Normal file
18
Task/Pi/Perl/pi-4.pl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use ntheory qw/Pi/;
|
||||
say Pi(10000);
|
||||
|
||||
use Math::Pari qw/setprecision Pi/;
|
||||
setprecision(10000);
|
||||
say Pi;
|
||||
|
||||
use Math::MPFR;
|
||||
my $pi = Math::MPFR->new();
|
||||
Math::MPFR::Rmpfr_set_prec($pi, int(10000 * 3.322)+40);
|
||||
Math::MPFR::Rmpfr_const_pi($pi, 0);
|
||||
say Math::MPFR::Rmpfr_get_str($pi, 10, 10000, 0);
|
||||
|
||||
use Math::BigFloat try=>"GMP"; # Slow without Math::BigInt::GMP installed
|
||||
say Math::BigFloat::bpi(10000); # For over ~2k digits, slower than AGM
|
||||
|
||||
use Math::Big qw/pi/; # Very slow
|
||||
say pi(10000);
|
||||
Loading…
Add table
Add a link
Reference in a new issue