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 ,
, and . Additional custom routines for exponentiation are used to ensure all computations are done with rationals, specifically FatRats (rational numbers stored with arbitrary size numerator and denominator). The module Rat::Precise makes it simple to display these to a configurable precision.
Output:
Continued fractions
Output:
Ramanujan's constant can also be generated to an arbitrary precision using standard for each component of the 𝑒**(π*√163) expression. Substantially slower than the first method.
Ramanujan's constant to 32 decimal places:
Actual: 262537412640768743.99999999999925007259719818568888
Calculated: 262537412640768743.99999999999925007259719818568888
Heegner numbers yielding 'almost' integers
Int 885480
Heegner 885479.777680154319498
Difference 0.222319845680502
Int 884736744
Heegner 884736743.999777466034907
Difference 0.000222533965093
Int 147197952744
Heegner 147197952743.999998662454225
Difference 0.000001337545775
Int 262537412640768744
Heegner 262537412640768743.999999999999250
Difference 0.000000000000750
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);
Ramanujan's constant to 32 decimal places:
262537412640768743.99999999999925007259719818568888