Ramanujan's constant: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: alternate calculation using continued fractions) |
SqrtNegInf (talk | contribs) (→{{header|Perl}}: calculation via continued fractions) |
||
Line 77: | Line 77: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
===Direct calculation=== |
|||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
<lang perl>use strict; |
<lang perl>use strict; |
||
Line 116: | Line 117: | ||
163: 262537412640768743.99999999999925007259719818568888 ≈ 262537412640768744 (diff: 0.00000000000074992740280181431112) |
163: 262537412640768743.99999999999925007259719818568888 ≈ 262537412640768744 (diff: 0.00000000000074992740280181431112) |
||
</pre> |
</pre> |
||
===Continued fractions=== |
|||
{{trans|Perl 6}} |
|||
<lang perl>use strict; |
|||
use Math::AnyNum <as_dec rat>; |
|||
sub continued_fr { |
|||
my ($a, $b, $n) = (@_[0,1], $_[2] // 100); |
|||
$a->() + ($n && $b->() / continued_fr($a, $b, $n-1)); |
|||
} |
|||
my $r163 = continued_fr do {my $n; sub {$n++ ? 2*12 : 12 }}, do {my $n; sub { rat 19 }}, 40; |
|||
my $pi = continued_fr do {my $n; sub {$n++ ? 1 + 2*($n-2) : 0 }}, do {my $n; sub { rat($n++ ? ($n>2 ? ($n-1)**2 : 1) : 4)}}, 140; |
|||
my $p = $pi * $r163; |
|||
my $R = 1 + $p / continued_fr do { my $n; sub { $n++ ? $p+($n+0) : 1 } }, do {my $n; sub { $n++; -1*$n*$p }}, 180; |
|||
printf "Ramanujan's constant\n%s\n", as_dec($R,58); |
|||
</lang> |
|||
{{out}} |
|||
<pre>Ramanujan's constant |
|||
262537412640768743.9999999999992500725971981856888793538563</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |