Skip to content

Instantly share code, notes, and snippets.

@grondilu
Last active January 3, 2016 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/8387779 to your computer and use it in GitHub Desktop.
Save grondilu/8387779 to your computer and use it in GitHub Desktop.
computing pi in Perl 6
use v6;
constant number-of-decimals = 100;
sub my-sqrt($n) {
my Int $square = (10**(2*number-of-decimals) * $n).round;
return FatRat.new: .[*-1], 10**number-of-decimals given
1, { ($_ + $square div $_) div 2 } ... * == *
}
my ($a, $n) = 1, 1;
my $g = my-sqrt(1/2);
my $z = .25;
for ^10 {
given [ ($a + $g)/2, my-sqrt($a * $g) ] {
$z -= (.[0] - $a)**2 * $n;
$n += $n;
($a, $g) = @$_
}
}
say $a ** 2 / $z;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment