Ramanujan's constant: Difference between revisions

→‎{{header|Perl 6}}: alternate calculation using continued fractions
m (→‎{{header|REXX}}: elided the ROOT function.)
(→‎{{header|Perl 6}}: alternate calculation using continued fractions)
Line 118:
 
=={{header|Perl 6}}==
===Iterative calculations===
To generate a high-precision value for Ramanujan's constant, code is borrowed from three other Rosettacode tasks
(with some modifications) for performing calculations of
Line 234 ⟶ 235:
Heegner 262537412640768743.999999999999250
Difference 0.000000000000750</pre>
 
===Continued fractions===
Ramanujan's constant can also be generated to an arbitrary precision using standard [https://en.wikipedia.org/wiki/Generalized_continued_fraction continued fraction formulas] for each component of the 𝑒**(π*√163) expression. Substantially slower than the first method.
<lang perl6>use Rat::Precise;
 
sub continued-fraction($n, :@a, :@b) {
my $x = @a[0].FatRat;
$x = @a[$_ - 1] + @b[$_] / $x for reverse 1 ..^ $n;
$x;
}
 
#`{ √163 } my $r163 = continued-fraction( 50, :a(12,|((2*12) xx *)), :b(19 xx *));
#`{ π } my $pi = 4*continued-fraction(140, :a( 0,|(1, 3 ... *)), :b(4, 1, |((1, 2, 3 ... *) X** 2)));
#`{ e**x } my $R = 1 + ($_ / continued-fraction(170, :a( 1,|(2+$_, 3+$_ ... *)), :b(Nil, |(-1*$_, -2*$_ ... *) ))) given $r163*$pi;
 
say "Ramanujan's constant to 32 decimal places:\n", $R.precise(32);</lang>
{{out}}
<pre>Ramanujan's constant to 32 decimal places:
262537412640768743.99999999999925007259719818568888</pre>
 
=={{header|REXX}}==
2,392

edits