Last active
January 3, 2016 01:09
-
-
Save grondilu/8387779 to your computer and use it in GitHub Desktop.
computing pi in Perl 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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