Calculate Ramanujan's constant (as described on the OEIS site) with at least 32 digits of precision, by the method of your choice. Optionally, if using the 𝑒**(Ο€*√x) approach, show that when evaluated with the last four Heegner numbers the result is almost an integer.

Task
Ramanujan's constant
You are encouraged to solve this task according to the task description, using any language you may know.

Fōrmulæ

In this page you can see the solution of this task.

FōrmulΓ¦ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text (more info). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation β€”i.e. XML, JSONβ€” they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.

Go

Library: bigfloat

The standard library's math/big.Float type lacks an exponentiation function and so I have had to use an external library to provide this function.

Also the math.Pi built in constant is not accurate enough to be used with big.Float and so I have used a more accurate string representation instead. <lang go>package main

import (

   "fmt"
   "github.com/ALTree/bigfloat"
   "math/big"

)

const (

   prec = 256 // say
   ps   = "3.1415926535897932384626433832795028841971693993751058209749445923078164"

)

func q(d int64) *big.Float {

   pi, _Β := new(big.Float).SetPrec(prec).SetString(ps)
   tΒ := new(big.Float).SetPrec(prec).SetInt64(d)
   t.Sqrt(t)
   t.Mul(pi, t)
   return bigfloat.Exp(t)

}

func main() {

   fmt.Println("Ramanujan's constant to 32 decimal places is:")
   fmt.Printf("%.32f\n", q(163))
   heegnersΒ := [4][2]int64{
       {19, 96},
       {43, 960},
       {67, 5280},
       {163, 640320},
   }
   fmt.Println("\nHeegner numbers yielding 'almost' integers:")
   tΒ := new(big.Float).SetPrec(prec)
   for _, hΒ := range heegners {
       qhΒ := q(h[0])
       cΒ := h[1]*h[1]*h[1] + 744
       t.SetInt64(c)
       t.Sub(t, qh)
       fmt.Printf("%3d: %51.32f β‰ˆ %18d (diff:Β %.32f)\n", h[0], qh, c, t)
   }

}</lang>

Output:
Ramanujan's constant to 32 decimal places is:
262537412640768743.99999999999925007259719818568888

Heegner numbers yielding 'almost' integers:
 19:             885479.77768015431949753789348171962682 β‰ˆ             885480 (diff: 0.22231984568050246210651828037318)
 43:          884736743.99977746603490666193746207858538 β‰ˆ          884736744 (diff: 0.00022253396509333806253792141462)
 67:       147197952743.99999866245422450682926131257863 β‰ˆ       147197952744 (diff: 0.00000133754577549317073868742137)
163: 262537412640768743.99999999999925007259719818568888 β‰ˆ 262537412640768744 (diff: 0.00000000000074992740280181431112)

J

Project: compute, expressed in mathematica then j notation,

Exp[Pi*Sqrt[163]]    ^ o.Β %: 163

.

J natively supports Boolean 0 1, integer 00 01 2 3 9, extended integer* 9x, rational 1r2, floating point as c double, and complex numbers 2ad90 (radius 2, 90 angle in degrees). J does not natively support arbitrary precision decimal, or ternary. J can format a rationals as arbitrary precision base 10 literals.

Rational arithmetic with series expansion therefor serves to compute Ramanujan's constant. We test for convergence in the base 10 literal expression over the required length. Exponential expansion is unwieldly. We divide by 8x, then raise the series sum to the 8th power. We also convert the rational exponent to a base 10 rational number of sufficient digits to reduce size.

  • Taking a rational power of an extended integer produces a floating-point result whenever the denominator of the power is not 1.
Note 'citation for pi computation'
 @MISC {3129700,
     TITLE = {Series that converge to $\pi$ quickly},
     AUTHOR = {El Ectric (https://math.stackexchange.com/users/301661/el-ectric)},
     HOWPUBLISHED = {Mathematics Stack Exchange},
     NOTE = {URL:https://math.stackexchange.com/q/3129700 (version: 2019-02-28)},
     EPRINT = {https://math.stackexchange.com/q/3129700},
     URL = {https://math.stackexchange.com/q/3129700}
 }
)


NB. returns u to about y significant digits
Digits=: adverb define NB. u Digits y    u y  is less accurate than  u y+1
 format=. ((j.~ 50&+) y)&":
 i =. 5
 current=. format u i
 whilst. last -.@-: current do.
  last =. current
  i =. i + 2
  current=. format result =. u i
 end.
 result
)

NB. pi is sufficiently fast for partial series recomputation.
numerator=: (*&! +:) * _3 25&p.
denominator=:  2&^ *Β !@:(3&*)
pi=: (2 * [: +/ numeratorΒ % denominator)@:i.@:x:  NB. use: pi TERMS

cf_sqrt=: 10&$:Β :(4 :0)  NB. continued fraction approximation to square root. x sqrt y then x is the depth, y the square
 a =. x: <.Β %: y NB. estimate
 r =. y - *: a   NB. remainder
 a +Β %`+/ (+: x) $ r , +: a
)

Note 'exp'
 exp=:10&$:Β :(([: +/ ((^Β %Β !@:]) i.@:x:))~)

 x exp y  sums x terms of Taylor series for Exp[y]
)

NB. combines digits with exp terms.
exp=: 1&$:Β :(4Β : 0)
 format=. ((j.~ 50&+) x)&":
 ex =. term=. 0x
 current=. ''
 whilst. last -.@-: current do.
  last =. current
  ex =. ex + y (^Β %Β !@:]) term
  current=. format ex
  term =. >: term
 end.
 ex
)

decimal_fraction=: 3 :0  NB. decimal_fraction literal
 y=. y -. ' '
 i=. y (, i. ]) '.'
 I=. 'x' ,~ i {. y
 F=. 'x' ,~ y }.~ >: i
 I +&". (, 'Β % ' , 'x' ,~ '1' #!.'0'~ 1 j. <:@:#) F
)

<lang>

  NB. takes the constant beyond the repeat 9s.
  S=: cf_sqrt&163 Digits 34
  P=: pi Digits 34
  X=: decimal_fraction 37j34":1r8*P*S
  59j40":8^~0{::32 exp X

262537412640768743.9999999999992500541677354265492332557331

  NB.                             ^xxxxxxxxxxxxxxxxxxxxxxx

</lang>

Java

Very interesting. Compute Pi, E, and square root to arbitrary precision. <lang java> import java.math.BigDecimal; import java.math.MathContext; import java.util.Arrays; import java.util.List;

public class RamanujanConstant {

   public static void main(String[] args) {
       System.out.printf("Ramanujan's Constant to 100 digits = %s%n%n", ramanujanConstant(163, 100));
       System.out.printf("Heegner numbers yielding 'almost' integers:%n");
       List<Integer> heegnerNumbers = Arrays.asList(19, 43, 67, 163);
       List<Integer> heegnerVals = Arrays.asList(96, 960, 5280, 640320);
       for ( int i = 0Β ; i < heegnerNumbers.size()Β ; i++ ) {
           int heegnerNumber = heegnerNumbers.get(i);
           int heegnerVal = heegnerVals.get(i);
           BigDecimal integer = BigDecimal.valueOf(heegnerVal).pow(3).add(BigDecimal.valueOf(744));
           BigDecimal compute = ramanujanConstant(heegnerNumber, 50);
           System.out.printf("%3dΒ : %50s ~ %18s (diff ~ %s)%n", heegnerNumber, compute, integer, integer.subtract(compute, new MathContext(30)).toPlainString());
       }
   }
   
   public static BigDecimal ramanujanConstant(int sqrt, int digits) {
       //  For accuracy on lat digit, computations with a few extra digits
       MathContext mc = new MathContext(digits + 5);  
       return bigE(bigPi(mc).multiply(bigSquareRoot(BigDecimal.valueOf(sqrt), mc), mc), mc).round(new MathContext(digits));
   }
   //  e = 1 + x/1! + x^2/2! + x^3/3! + ...
   public static BigDecimal bigE(BigDecimal exponent, MathContext mc) {
       BigDecimal e = BigDecimal.ONE;
       BigDecimal ak = e;
       int k = 0;
       BigDecimal min = BigDecimal.ONE.divide(BigDecimal.TEN.pow(mc.getPrecision()));
       while ( true ) {
           k++;
           ak = ak.multiply(exponent).divide(BigDecimal.valueOf(k), mc);
           e = e.add(ak, mc);
           if ( ak.compareTo(min) < 0 ) {
               break;
           }
       }
       return e;
       
   }
   
   //  SeeΒ : https://www.craig-wood.com/nick/articles/pi-chudnovsky/  
   public static BigDecimal bigPi(MathContext mc) {
       int k = 0;
       BigDecimal ak = BigDecimal.ONE;
       BigDecimal a = ak;
       BigDecimal b = BigDecimal.ZERO;
       BigDecimal c = BigDecimal.valueOf(640320);
       BigDecimal c3 = c.pow(3);
       double digitePerTerm = Math.log10(c.pow(3).divide(BigDecimal.valueOf(24), mc).doubleValue()) - Math.log10(72);
       double digits = 0;
       while ( digits < mc.getPrecision() ) {
           k++;
           digits += digitePerTerm;
           BigDecimal top = BigDecimal.valueOf(-24).multiply(BigDecimal.valueOf(6*k-5)).multiply(BigDecimal.valueOf(2*k-1)).multiply(BigDecimal.valueOf(6*k-1));
           BigDecimal term = top.divide(BigDecimal.valueOf(k*k*k).multiply(c3), mc);
           ak = ak.multiply(term, mc);
           a = a.add(ak, mc);
           b = b.add(BigDecimal.valueOf(k).multiply(ak, mc), mc);
       }
       BigDecimal total = BigDecimal.valueOf(13591409).multiply(a, mc).add(BigDecimal.valueOf(545140134).multiply(b, mc), mc);
       return BigDecimal.valueOf(426880).multiply(bigSquareRoot(BigDecimal.valueOf(10005), mc), mc).divide(total, mc);
   }
   //  SeeΒ : https://en.wikipedia.org/wiki/Newton's_method#Square_root_of_a_number
   public static BigDecimal bigSquareRoot(BigDecimal squareDecimal, MathContext mc) {
       //  Estimate
       double sqrt = Math.sqrt(squareDecimal.doubleValue());
       BigDecimal x0 = new BigDecimal(sqrt, mc);
       BigDecimal two = BigDecimal.valueOf(2);
       while ( true ) {
           BigDecimal x1 = x0.subtract(x0.multiply(x0, mc).subtract(squareDecimal).divide(two.multiply(x0, mc), mc), mc);
           String x1String = x1.toPlainString();
           String x0String = x0.toPlainString();
           if ( x1String.substring(0, x1String.length()-1).compareTo(x0String.substring(0, x0String.length()-1)) == 0 ) {
               break;
           }
           x0 = x1;
       }
       return x0;
   }
   

} </lang>

Output:
Ramanujan's Constant to 100 digits = 262537412640768743.9999999999992500725971981856888793538563373369908627075374103782106479101186073130

Heegner numbers yielding 'almost' integers:
 19Β : 885479.77768015431949753789348171962682071428650186 ~             885480 (diff ~ 0.222319845680502462106518280373)
 43Β : 884736743.99977746603490666193746207858537684739913 ~          884736744 (diff ~ 0.000222533965093338062537921414623)
 67Β : 147197952743.99999866245422450682926131257862850818 ~       147197952744 (diff ~ 0.00000133754577549317073868742137149)
163Β : 262537412640768743.99999999999925007259719818568888 ~ 262537412640768744 (diff ~ 0.00000000000074992740280181431112)

Julia

<lang julia>

julia> a = BigFloat(MathConstants.e^(BigFloat(pi)))^(BigFloat(163.0)^0.5) 2.625374126407687439999999999992500725971981856888793538563373369908627075373427e+17

julia> 262537412640768744 - a 7.499274028018143111206461436626630091372924626572825942241598957614307213309258e-13

</lang>

Perl

Direct calculation

Translation of: Sidef

<lang perl>use strict; use warnings; use Math::AnyNum;

sub ramanujan_const {

   my ($x, $decimals) = @_;
   $x = Math::AnyNum->new($x);
   my $prec = (Math::AnyNum->pi * $x->sqrt)/log(10) + $decimals + 1;
   local $Math::AnyNum::PREC = 4*$prec->round->numify;
   exp(Math::AnyNum->pi * $x->sqrt)->round(-$decimals)->stringify;

}

my $decimals = 100; printf("Ramanujan's constant to $decimals decimals:\n%s\n\n",

   ramanujan_const(163, $decimals));

print "Heegner numbers yielding 'almost' integers:\n"; my @tests = (19, 96, 43, 960, 67, 5280, 163, 640320);

while (@tests) {

   my ($h, $x) = splice(@tests, 0, 2);
   my $c = ramanujan_const($h, 32);
   my $n = Math::AnyNum::ipow($x, 3) + 744;
   printf("%3s: %51s β‰ˆ %18s (diff: %s)\n", $h, $c, $n, ($n - $c)->round(-32));

}</lang>

Output:
Ramanujan's constant to 100 decimals:
262537412640768743.9999999999992500725971981856888793538563373369908627075374103782106479101186073129511813461860645042

Heegner numbers yielding 'almost' integers:
 19:             885479.77768015431949753789348171962682 β‰ˆ             885480 (diff: 0.22231984568050246210651828037318)
 43:          884736743.99977746603490666193746207858538 β‰ˆ          884736744 (diff: 0.00022253396509333806253792141462)
 67:       147197952743.99999866245422450682926131257863 β‰ˆ       147197952744 (diff: 0.00000133754577549317073868742137)
163: 262537412640768743.99999999999925007259719818568888 β‰ˆ 262537412640768744 (diff: 0.00000000000074992740280181431112)

Continued fractions

Translation of: Raku

<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>

Output:
Ramanujan's constant
262537412640768743.9999999999992500725971981856888793538563

Phix

Translation of: Go
Library: mpfr

<lang Phix>include mpfr.e mpfr_set_default_prec(-120) -- (18 before, 100 after, plus 2 for kicks.)

function q(integer d)

   mpfr pi = mpfr_init()
   mpfr_const_pi(pi)
   mpfr t = mpfr_init(d)
   mpfr_sqrt(t,t)
   mpfr_mul(t,pi,t)
   mpfr_exp(t,t)
   return t

end function

printf(1,"Ramanujan's constant to 100 decimal places is:\n") mpfr_printf(1, "%.100Rf\n", q(163)) sequence heegners = {{19, 96},

                    {43, 960},
                    {67, 5280},
                    {163, 640320},
                   }

printf(1,"\nHeegner numbers yielding 'almost' integers:\n") mpfr t = mpfr_init(), qh mpz c = mpz_init() for i=1 to length(heegners) do

   integer {h0,h1} = heegners[i]
   qh = q(h0)
   mpz_ui_pow_ui(c,h1,3)
   mpz_add_ui(c,c,744)
   mpfr_set_z(t,c)
   mpfr_sub(t,t,qh)
   string qhs = mpfr_sprintf("%51.32Rf",qh),
          cs = mpz_get_str(c),
          ts = mpfr_sprintf("%.32Rf",t)
   printf(1,"%3d: %s ~= %18s (diff: %s)\n", {h0, qhs, cs, ts})

end for</lang>

Output:
Ramanujan's constant to 100 decimal places is:
262537412640768743.9999999999992500725971981856888793538563373369908627075374103782106479101186073129511813461860645042

Heegner numbers yielding 'almost' integers:
 19:             885479.77768015431949753789348171962682 ~=             885480 (diff: 0.22231984568050246210651828037318)
 43:          884736743.99977746603490666193746207858538 ~=          884736744 (diff: 0.00022253396509333806253792141462)
 67:       147197952743.99999866245422450682926131257863 ~=       147197952744 (diff: 0.00000133754577549317073868742137)
163: 262537412640768743.99999999999925007259719818568888 ~= 262537412640768744 (diff: 0.00000000000074992740280181431112)

Raku

(formerly 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 the value of Ο€, Euler's number, and integer roots. 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. <lang perl6>use Rat::Precise;

  1. set the degree of precision for calculations

constant D = 54; constant d = 15;

  1. two versions of exponentiation where base and exponent are both FatRat

multi infix:<**> (FatRat $base, FatRat $exp where * >= 1 --> FatRat) {

   2 R** $base**($exp/2);

}

multi infix:<**> (FatRat $base, FatRat $exp where * < 1 --> FatRat) {

   constant Ξ΅ = 10**-D;
   my $low  = 0.FatRat;
   my $high = 1.FatRat;
   my $mid  = $high / 2;
   my $acc  = my $sqr = sqrt($base);
   while (abs($mid - $exp) > Ξ΅) {
     $sqr = sqrt($sqr);
     if ($mid <= $exp) { $low  = $mid; $acc *=   $sqr }
     else              { $high = $mid; $acc *= 1/$sqr }
     $mid = ($low + $high) / 2;
   }
   $acc.substr(0, D).FatRat;

}

  1. calculation of Ο€

sub Ο€ (--> FatRat) {

   my ($a, $n) = 1, 1;
   my $g = sqrt 1/2.FatRat;
   my $z = .25;
   my $pi;
   for ^d {
       given [ ($a + $g)/2, sqrt $a * $g ] {
           $z -= (.[0] - $a)**2 * $n;
           $n += $n;
           ($a, $g) = @$_;
           $pi = ($a ** 2 / $z).substr: 0, 2 + D;
       }
   }
   $pi.FatRat;

}

multi sqrt(FatRat $r --> FatRat) {

   FatRat.new: sqrt($r.nude[0] * 10**(D*2) div $r.nude[1]), 10**D;

}

  1. integer roots

multi sqrt(Int $n) {

   my $guess = 10**($n.chars div 2);
   my $iterator = { ( $^x + $n div ($^x) ) div 2 };
   my $endpoint = { $^x == $^y|$^z };
   min ($guess, $iterator … $endpoint)[*-1, *-2];

}

  1. 'cosmetic' cover to upgrade input to FatRat sqrt

sub prefix:<√> (Int $n) { sqrt($n.FatRat) }

  1. calculation of 𝑒

sub postfix:<!> (Int $n) { (constant f = 1, |[\*] 1..*)[$n] } sub 𝑒 (--> FatRat) { sum map { FatRat.new(1,.!) }, ^D }

  1. inputs, and their difference, formatted decimal-aligned

sub format ($a,$b) {

   sub pad ($s) { ' ' x ((34 - d - 1) - ($s.split(/\./)[0]).chars) }
   my $c = $b.precise(d, :z);
   my $d = ($a-$b).precise(d, :z);
   join "\n",
       (sprintf "%11s {pad($a)}%s\n", 'Int', $a) ~
       (sprintf "%11s {pad($c)}%s\n", 'Heegner', $c) ~
       (sprintf "%11s {pad($d)}%s\n", 'Difference', $d)

}

  1. override built-in definitions

constant Ο€ = &Ο€(); constant 𝑒 = &𝑒();

my $Ramanujan = 𝑒**(Ο€*√163); say "Ramanujan's constant to 32 decimal places:\nActual: " ~

   "262537412640768743.99999999999925007259719818568888\n" ~
   "Calculated: ", $Ramanujan.precise(32, :z), "\n";

say "Heegner numbers yielding 'almost' integers"; for 19, 96, 43, 960, 67, 5280, 163, 640320 -> $heegner, $x {

   my $almost = 𝑒**(Ο€*√$heegner);
   my $exact  = $x**3 + 744;
   say format($exact, $almost);

}</lang>

Output:
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

Continued fractions

Ramanujan's constant can also be generated to an arbitrary precision using standard 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;

}

  1. `{ √163 } my $r163 = continued-fraction( 50, :a(12,|((2*12) xx *)), :b(19 xx *));
  2. `{ Ο€ } my $pi = 4*continued-fraction(140, :a( 0,|(1, 3 ... *)), :b(4, 1, |((1, 2, 3 ... *) X** 2)));
  3. `{ 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>

Output:
Ramanujan's constant to 32 decimal places:
262537412640768743.99999999999925007259719818568888

REXX

Instead of calculating Β  e Β  and Β    Β  to some arbitrary length, Β  it was easier to just include those two constants with Β  201 Β  decimal digits Β  (which is the amount of decimal digits used for the calculations). Β  The results are displayed Β  (right justified) Β  with one half of that number of decimal digits past the decimal point. <lang rexx>/*REXX pgm displays Ramanujan's constant to at least 100 decimal digits of precision. */ d= min( length(pi()), length(e()) ) - length(.) /*calculate max #decimal digs supported*/ parse arg digs sDigs . 1 . . $ /*obtain optional arguments from the CL*/ if digs== | digs=="," then digs= d /*Not specified? Then use the default.*/ if sDigs== | sDigs=="," then sDigs= dΒ % 2 /* " " " " " " */ if $= | $="," then $= 19 43 67 163 /* " " " " " " */

digs= min( digs, d)                             /*the minimum decimal digs for calc.   */

sDigs= min(sDigs, d) /* " " " " display.*/ numeric digits digs /*inform REXX how many dec digs to use.*/ say "The value of Ramanujan's constant calculated with " d ' decimal digits of precision.' say "shown with " sDigs ' decimal digits past the decimal point:' say

      do  j=1  for words($);   #= word($, j)    /*process each of the Heegner numbers. */
      say 'When using the Heegner number: '  #  /*display which Heegner # is being used*/
      z= exp(pi * sqrt(#) )                     /*perform some heavy lifting here.     */
      say format(z, 25, sDigs);           say   /*display a limited amount of dec digs.*/
      end   /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862,

          || 089986280348253421170679821480865132823066470938446095505822317253594081284,
          || 8111745028410270193852110555964462294895493038196;     return pi

/*──────────────────────────────────────────────────────────────────────────────────────*/ e: e = 2.7182818284590452353602874713526624977572470936999595749669676277240766303535,

          || 475945713821785251664274274663919320030599218174135966290435729003342952605,
          || 9563073813232862794349076323382988075319525101901;     return  e

/*──────────────────────────────────────────────────────────────────────────────────────*/ exp: procedure; parse arg x; ix= x%1; if abs(x-ix)>.5 then ix= ix + sign(x); x= x-ix

      z=1;  _=1;   w=z;     do j=1; _= _*x/j;  z=(z+_)/1; if z==w  then leave;  w=z;  end
      if z\==0  then z= z * e() ** ix;                                         return z/1

/*──────────────────────────────────────────────────────────────────────────────────────*/ sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); h=d+6; numeric digits

      numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g 'E' _ .;  g=g*.5'e'_%2
        do j=0  while h>9;      m.j=h;               h=hΒ % 2  +  1;   end /*j*/
        do k=j+5  to 0  by -1;  numeric digits m.k;  g=(g+x/g) * .5;  end /*k*/; return g</lang>
output Β  when using the default inputs:
The value of Ramanujan's constant calculated with  201  decimal digits of precision.
shown with  100  decimal digits past the decimal point:

When using the Heegner number:  19
                   885479.7776801543194975378934817196268207142865018553571526577110128809842286637202423189990118182067775711

When using the Heegner number:  43
                884736743.9997774660349066619374620785853768473991271391609175146278344881148747592189635643106023717101372606

When using the Heegner number:  67
             147197952743.9999986624542245068292613125786285081833125038167126333712821051229509988315235020413792423533706290

When using the Heegner number:  163
       262537412640768743.9999999999992500725971981856888793538563373369908627075374103782106479101186073129511813461860645042

Sidef

<lang ruby>func ramanujan_const(x, decimals=32) {

   local Num!PREC = *"#{4*round((Num.pi*√x)/log(10) + decimals + 1)}"
   exp(Num.pi * √x) -> round(-decimals).to_s

}

var decimals = 100 printf("Ramanujan's constant to #{decimals} decimals:\n%s\n\n",

    ramanujan_const(163, decimals))

say "Heegner numbers yielding 'almost' integers:" [19, 96, 43, 960, 67, 5280, 163, 640320].each_slice(2, {|h,x|

   var c = ramanujan_const(h, 32)
   var n = (x**3 + 744)
   printf("%3s: %51s β‰ˆ %18s (diff: %s)\n", h, c, n, n-Num(c))

})</lang>

Output:
Ramanujan's constant to 100 decimals:
262537412640768743.9999999999992500725971981856888793538563373369908627075374103782106479101186073129511813461860645042

Heegner numbers yielding 'almost' integers:
 19:             885479.77768015431949753789348171962682 β‰ˆ             885480 (diff: 0.22231984568050246210651828037318)
 43:          884736743.99977746603490666193746207858538 β‰ˆ          884736744 (diff: 0.00022253396509333806253792141462)
 67:       147197952743.99999866245422450682926131257863 β‰ˆ       147197952744 (diff: 0.00000133754577549317073868742137)
163: 262537412640768743.99999999999925007259719818568888 β‰ˆ 262537412640768744 (diff: 0.00000000000074992740280181431112)