Ramanujan's constant: Difference between revisions

m
m (→‎{{header|Phix}}: 32 ==> 100 d.p.)
 
(37 intermediate revisions by 15 users not shown)
Line 1:
{{draft task}}
 
Calculate Ramanujan's constant (as described on the [http://oeis.org/wiki/Ramanujan%27s_constant OEIS site]) with at least
Line 5:
show that when evaluated with the last four [https://en.wikipedia.org/wiki/Heegner_number Heegner numbers]
the result is ''almost'' an integer.
 
=={{header|C++}}==
{{libheader|Boost}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
 
using big_float = boost::multiprecision::cpp_dec_float_100;
 
big_float f(unsigned int n) {
big_float pi(boost::math::constants::pi<big_float>());
return exp(sqrt(big_float(n)) * pi);
}
 
int main() {
std::cout << "Ramanujan's constant using formula f(N) = exp(pi*sqrt(N)):\n"
<< std::setprecision(80) << f(163) << '\n';
std::cout << "\nResult with last four Heegner numbers:\n";
std::cout << std::setprecision(30);
for (unsigned int n : {19, 43, 67, 163}) {
auto x = f(n);
auto c = ceil(x);
auto pc = 100.0 * (x/c);
std::cout << "f(" << n << ") = " << x << " = "
<< pc << "% of " << c << '\n';
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Ramanujan's constant using formula f(N) = exp(pi*sqrt(N)):
262537412640768743.99999999999925007259719818568887935385633733699086270753741038
 
Result with last four Heegner numbers:
f(19) = 885479.777680154319497537893482 = 99.9999748927309842681413350366% of 885480
f(43) = 884736743.999777466034906661937 = 99.9999999999748474372063224648% of 884736744
f(67) = 147197952743.999998662454224507 = 99.9999999999999990913285473342% of 147197952744
f(163) = 262537412640768743.999999999999 = 99.9999999999999999999999999997% of 262537412640768744
</pre>
 
=={{header|Fōrmulæ}}==
 
In [{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Ramanujan%27s_constant this] page you can see the solution of this task.}}
 
'''Solution'''
 
'''Case 1. Calculate Ramanujan's constant with at least 32 digits of precision'''
 
[[File:Fōrmulæ - Ramanujan's constant 01.png]]
 
[[File:Fōrmulæ - Ramanujan's constant 02.png]]
 
'''Case 2. Show that when evaluated with the last four Heegner numbers the result is almost an integer'''
 
[[File:Fōrmulæ - Ramanujan's constant 03.png]]
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
[[File:Fōrmulæ - Ramanujan's constant 04.png]]
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 19 ⟶ 70:
 
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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 58 ⟶ 109:
fmt.Printf("%3d: %51.32f ≈ %18d (diff: %.32f)\n", h[0], qh, c, t)
}
}</langsyntaxhighlight>
 
{{out}}
Line 71 ⟶ 122:
163: 262537412640768743.99999999999925007259719818568888 ≈ 262537412640768744 (diff: 0.00000000000074992740280181431112)
</pre>
 
=={{header|Haskell}}==
Calculations are done using an arbitrary precision "constructive reals" type (CReal).
<syntaxhighlight lang="haskell">import Control.Monad (forM_)
import Data.Number.CReal (CReal, showCReal)
import Text.Printf (printf)
 
ramfun :: CReal -> CReal
ramfun x = exp (pi * sqrt x)
 
-- Ramanujan's constant.
ramanujan :: CReal
ramanujan = ramfun 163
 
-- The last four Heegner numbers.
heegners :: [Int]
heegners = [19, 43, 67, 163]
 
-- The absolute distance to the nearest integer.
intDist :: CReal -> CReal
intDist x = abs (x - fromIntegral (round x))
 
main :: IO ()
main = do
let n = 35
printf "Ramanujan's constant: %s\n\n" (showCReal n ramanujan)
printf "%3s %34s%20s%s\n\n" " h " "e^(pi*sqrt(h))" "" " Dist. to integer"
forM_ heegners $ \h ->
let r = ramfun (fromIntegral h)
d = intDist r
in printf "%3d %54s %s\n" h (showCReal n r) (showCReal 15 d)</syntaxhighlight>
 
{{out}}
<pre>
Ramanujan's constant: 262537412640768743.99999999999925007259719818568887935
 
h e^(pi*sqrt(h)) Dist. to integer
 
19 885479.77768015431949753789348171962682071 0.222319845680502
43 884736743.99977746603490666193746207858537685 0.000222533965093
67 147197952743.99999866245422450682926131257862851 0.000001337545775
163 262537412640768743.99999999999925007259719818568887935 0.00000000000075
</pre>
 
=={{header|J}}==
Project: compute, expressed in mathematica then j notation, <pre>Exp[Pi*Sqrt[163]] ^ o. %: 163</pre> .
 
J natively supports arithmetic types Boolean 0 1, integer 00 01 2 3 9, extended integer<ref>Taking a rational power of an extended integer produces a floating-point result whenever the denominator of the power is not 1.</ref> 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 rational 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 of "long" rational numbers and the number of terms needed for "large" numbers is unwieldly. We divide by 8x, then raise the series sum to the 8th power. We also convert the rational exponent to a rational number of sufficient digits to reduce size. Tolerant continued fraction expansion reduces the magnitude of the exponent's numerator and denominator.
 
{{reflist}}
===continued fraction===
<pre>
NB. approximation as a rational number
 
]RC=: +`%/ }: 1j1 (#!.1) 262537412640768743x 1 1333462407511 1 8 1 1 5 1 4 1 7 1 1 1 9 1 1 2 12 4 1 15 4 299 3 5 1 4 5 5 1 28 3 1 9 4 1 6 1 1 1 1 1 1 51 11 5 3 2 1 1 1 1 2 1 5 1 9 1x
45120712325606158012363304056579024470785114628332049030433r171863933112440071790625667019825998411698
 
NB. expressed in decimal
59j40 ": RC
262537412640768743.9999999999992500725971981856888793538563
</pre>
 
=== 𝑒**(π*√x) ===
<pre>
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}
}
)
 
Digits=: adverb define NB. u Digits y u y is less accurate than u y+1
NB. returns u to at least y significant digits
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
)
 
cf=: 0.1&$: :(4 :0) NB. tolerance cf value -> continued fraction approximation of value to tolerance
Y =. y
X =. 0 >. x
terms =. 0 $ 0x
whilst. X < | approximation - y do.
'term Y' =. <.`([:%1&|)`:0 Y
terms =. terms , term
approximation =. +`%/ }: 1j1 #!.1 terms
end.
)
 
assert (-: 0&cf) 649r200
assert 13r4 (-: cf) 649r200
 
 
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
)
 
exp=: (1x"_)`((($:~<:)+^%!@x:@])~)@.(0<[) NB. recursive Taylor series x exp y recursively sums x terms of Taylor series for Exp[y], memoization candidate.
</pre>
<syntaxhighlight lang="text">
NB. takes the constant beyond the repeat 9s.
S=: cf_sqrt&163 Digits 34
P=: pi Digits 34
Y=: 1e_36 cf 1r8*P*S
f=: exp&Y M. NB. memoize
59j40 ": 8 ^~ f Digits 34
262537412640768743.9999999999992500711164316586918409066184
NB. ^
</syntaxhighlight>
 
=={{header|Java}}==
Very interesting. Compute Pi, E, and square root to arbitrary precision.
<langsyntaxhighlight lang="java">
import java.math.BigDecimal;
import java.math.MathContext;
Line 162 ⟶ 341:
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 176 ⟶ 355:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
 
julia> a = BigFloat(MathConstants.e^(BigFloat(pi)))^(BigFloat(163.0)^0.5)
Line 184 ⟶ 363:
7.499274028018143111206461436626630091372924626572825942241598957614307213309258e-13
 
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">First[RealDigits[N[Exp[Pi Sqrt[163]], 200]]]
Table[
c = N[Exp[Pi Sqrt[h]], 40];
Log10[1 - FractionalPart[c]]
,
{h, {19, 43, 67, 163}}
]</syntaxhighlight>
{{out}}
<pre>{2,6,2,5,3,7,4,1,2,6,4,0,7,6,8,7,4,3,9,9,9,9,9,9,9,9,9,9,9,9,2,5,0,0,7,2,5,9,7,1,9,8,1,8,5,6,8,8,8,7,9,3,5,3,8,5,6,3,3,7,3,3,6,9,9,0,8,6,2,7,0,7,5,3,7,4,1,0,3,7,8,2,1,0,6,4,7,9,1,0,1,1,8,6,0,7,3,1,2,9,5,1,1,8,1,3,4,6,1,8,6,0,6,4,5,0,4,1,9,3,0,8,3,8,8,7,9,4,9,7,5,3,8,6,4,0,4,4,9,0,5,7,2,8,7,1,4,4,7,7,1,9,6,8,1,4,8,5,2,3,2,2,4,3,2,0,3,9,1,1,6,4,7,8,2,9,1,4,8,8,6,4,2,2,8,2,7,2,0,1,3,1,1,7,8,3,1,7,0,6}
 
(*Log10 of the difference between the number and an integer*)
{-0.653021767688625734085368753068345, -3.652603693775839429642336360, -5.87369134597671206721205, -12.1249807767}</pre>
 
=={{header|Nim}}==
{{libheader|nim-decimal}}
<syntaxhighlight lang="nim">import strformat, strutils
import decimal
 
setPrec(75)
let pi = newDecimal("3.1415926535897932384626433832795028841971693993751058209749445923078164")
 
proc eval(n: int): DecimalType =
result = exp(pi * sqrt(newDecimal(n)))
 
func format(n: DecimalType; d: Positive): string =
## Return the representation of "n" with "d" digits of precision.
let parts = ($n).split('.')
result = parts[0] & '.' & parts[1][0..<d]
 
 
echo "Ramanujan’s constant with 50 digits of precision:"
echo eval(163).format(50)
 
setPrec(50)
echo()
echo "Heegner numbers yielding 'almost' integers:"
for n in [19, 43, 67, 163]:
let x = eval(n)
let k = x.roundToInt
let d = x - k
let s = if d > 0: "+ " & $d else: "- " & $(-d)
echo &"{n:3}: {x}... = {k:>18} {s}..."</syntaxhighlight>
 
{{out}}
<pre>Ramanujan’s constant with 50 digits of precision:
262537412640768743.99999999999925007259719818568887935385633733699086
 
Heegner numbers yielding 'almost' integers:
19: 885479.77768015431949753789348171962682071428650216... = 885480 - 0.22231984568050246210651828037317928571349784...
43: 884736743.99977746603490666193746207858537684739914... = 884736744 - 0.00022253396509333806253792141462315260086...
67: 147197952743.99999866245422450682926131257862850810... = 147197952744 - 0.00000133754577549317073868742137149190...
163: 262537412640768743.99999999999925007259719818568865... = 262537412640768744 - 7.4992740280181431135e-13...</pre>
 
=={{header|Pari/GP}}==
<syntaxhighlight lang="parigp">\p 50
exp(Pi*sqrt(163))</syntaxhighlight>
{{out}}
<pre>
262537412640768743.99999999999925007259719818568888
</pre>
 
=={{header|Perl}}==
===Direct calculation===
{{trans|Sidef}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::AnyNum;
Line 215 ⟶ 456:
my $n = Math::AnyNum::ipow($x, 3) + 744;
printf("%3s: %51s ≈ %18s (diff: %s)\n", $h, $c, $n, ($n - $c)->round(-32));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 229 ⟶ 470:
 
===Continued fractions===
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="perl">use strict;
use Math::AnyNum <as_dec rat>;
 
Line 244 ⟶ 485:
 
printf "Ramanujan's constant\n%s\n", as_dec($R,58);
</syntaxhighlight>
</lang>
{{out}}
<pre>Ramanujan's constant
262537412640768743.9999999999992500725971981856888793538563</pre>
 
=={{header|Perl 6Phix}}==
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- no mpfr_exp() under p2js (yet), sorry</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] has been renamed)</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">120</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (18 before, 100 after, plus 2 for kicks.)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mpfr_exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">t</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Ramanujan's constant to 100 decimal places is:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">163</span><span style="color: #0000FF;">),</span><span style="color: #000000;">100</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">heegners</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">96</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">43</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">960</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">67</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5280</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">163</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">640320</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">}</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nHeegner numbers yielding 'almost' integers:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">qh</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">heegners</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">heegners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">qh</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">744</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_set_z</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qh</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">qhs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qh</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">cs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3d: %51s ~= %18s (diff: %s)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">qhs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ts</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
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)
</pre>
 
=={{header|Python}}==
{{libheader|mpmath}}
<syntaxhighlight lang="python">from mpmath import mp
heegner = [19,43,67,163]
mp.dps = 50
x = mp.exp(mp.pi*mp.sqrt(163))
print("calculated Ramanujan's constant: {}".format(x))
print("Heegner numbers yielding 'almost' integers:")
for i in heegner:
print(" for {}: {} ~ {} error: {}".format(str(i),mp.exp(mp.pi*mp.sqrt(i)),round(mp.exp(mp.pi*mp.sqrt(i))),(mp.pi*mp.sqrt(i)) - round(mp.pi*mp.sqrt(i))))
</syntaxhighlight>
{{out}}
<pre>
calculated Ramanujan's constant: 262537412640768743.99999999999925007259719818568888
Heegner numbers yielding 'almost' integers:
for 19: 885479.77768015431949753789348171962682071428650187 ~ 885480 error: 0.30611510123230903757863689092534707729405221250933
for 43: 884736743.99977746603490666193746207858537684739915 ~ 884736744 error: -0.39919930568613989412676260444831671571796782935998
for 67: 147197952743.99999866245422450682926131257862850819 ~ 147197952744 error: -0.28495586484466040200154673774982799575003729030943
for 163: 262537412640768743.99999999999925007259719818568888 ~ 262537412640768736 error: 0.10916999113251975535008362290414005390053481224586
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
===Iterative calculations===
To generate a high-precision value for Ramanujan's constant, code is borrowed from three other Rosettacode tasks
Line 256 ⟶ 573:
[http://rosettacode.org/wiki/Calculating_the_value_of_e Euler's number], and
[http://rosettacode.org/wiki/Arithmetic-geometric_mean/Integer_roots integer roots]. Additional custom routines for exponentiation are used to ensure all computations are done with rationals, specifically <tt>FatRat</tt>s (rational numbers stored with arbitrary size numerator and denominator). The module <tt>Rat::Precise</tt> makes it simple to display these to a configurable precision.
<syntaxhighlight lang="raku" perl6line>use Rat::Precise;
 
# set the degree of precision for calculations
Line 276 ⟶ 593:
while (abs($mid - $exp) > ε) {
$sqr = sqrt($sqr);
if ($mid <= $exp) { $low = $mid; $acc *×= $sqr }
else { $high = $mid; $acc *××= 1/$sqr }
$mid = ($low + $high) / 2;
}
Line 291 ⟶ 608:
 
for ^d {
given [ ($a + $g)/2, sqrt $a *× $g ] {
$z -= (.[0] - $a)**2 *× $n;
$n += $n;
($a, $g) = @$_;
Line 302 ⟶ 619:
 
multi sqrt(FatRat $r --> FatRat) {
FatRat.new: sqrt($r.nude[0] *× 10**(D*2D×2) div $r.nude[1]), 10**D;
}
 
Line 317 ⟶ 634:
 
# calculation of 𝑒
sub postfix:<!> (Int $n) { (constant f = 1, |[\*×] 1..*)[$n] }
sub 𝑒 (--> FatRat) { sum map { FatRat.new(1,.!) }, ^D }
 
Line 335 ⟶ 652:
constant 𝑒 = &𝑒();
 
my $Ramanujan = 𝑒**(π* × √163);
say "Ramanujan's constant to 32 decimal places:\nActual: " ~
"262537412640768743.99999999999925007259719818568888\n" ~
Line 342 ⟶ 659:
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);
}</langsyntaxhighlight>
{{out}}
<pre>Ramanujan's constant to 32 decimal places:
Line 370 ⟶ 687:
===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.
<syntaxhighlight lang="raku" perl6line>use Rat::Precise;
 
sub continued-fraction($n, :@a, :@b) {
Line 382 ⟶ 699:
#`{ 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);</langsyntaxhighlight>
{{out}}
<pre>Ramanujan's constant to 32 decimal places:
262537412640768743.99999999999925007259719818568888</pre>
 
=={{header|Phix}}==
{{trans|Go}}
{{libheader|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>
{{out}}
<pre>
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)
</pre>
 
=={{header|REXX}}==
Instead of calculating &nbsp; <big> '''e''' </big> &nbsp; and &nbsp; <big><big><math>\pi</math></big></big> &nbsp; to some arbitrary length, &nbsp; it was easier to just include those two constants with &nbsp; '''201''' &nbsp; decimal digits &nbsp; (which is the amount of decimal digits used for the calculations). &nbsp; The results are displayed &nbsp; (right justified) &nbsp; with one -half of that number of decimal digits past the decimal point.
<langsyntaxhighlight 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*/
Line 473 ⟶ 740:
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 492 ⟶ 759:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require "bigdecimal/math"
include BigMath
 
e, pi = E(200), PI(200)
[19, 43, 67, 163].each do |x|
puts "#{x}: #{(e ** (pi * BigMath.sqrt(BigDecimal(x), 200))).round(100).to_s("F")}"
end
</syntaxhighlight>
{{out}}
<pre>19: 885479.7776801543194975378934817196268207142865018553571526577110128809842286637202423189990118182067775711
43: 884736743.9997774660349066619374620785853768473991271391609175146278344881148747592189635643106023717101372606
67: 147197952743.999998662454224506829261312578628508183312503816712633371282105122950998831523502041379242353370629
163: 262537412640768743.9999999999992500725971981856888793538563373369908627075374103782106479101186073129511813461860645042
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight 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
Line 507 ⟶ 789:
var n = (x**3 + 744)
printf("%3s: %51s ≈ %18s (diff: %s)\n", h, c, n, n-Num(c))
})</langsyntaxhighlight>
{{out}}
<pre>
Line 514 ⟶ 796:
 
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)
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
Wren has BigRat but not BigFloat which means we are lacking both a 'big' value for pi and an arbitrary precision exponential method.
 
I've therefore hard-coded a value for pi with 70 decimal places (more than enough for this task) and written a 'big' exponential function using the Taylor series for e(x). The latter requires a lot of iterations and is therefore quite slow (takes about 5 seconds to calculate the Ramanujan constant). However, this is acceptable for a scripting language such as Wren.
<syntaxhighlight lang="wren">import "./big" for BigRat
import "./fmt" for Fmt
 
var pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164"
var bigPi = BigRat.fromDecimal(pi)
 
var exp = Fn.new { |x, p|
var sum = x + 1
var prevTerm = x
var k = 2
var eps = BigRat.fromDecimal("0.5e-%(p)")
while (true) {
var nextTerm = prevTerm * x / k
sum = sum + nextTerm
if (nextTerm < eps) break
// speed up calculations by limiting precision to 'p' places
prevTerm = BigRat.fromDecimal(nextTerm.toDecimal(p))
k = k + 1
}
return sum
}
 
var ramanujan = Fn.new { |n, dp|
var e = bigPi * BigRat.new(n, 1).sqrt(70)
return exp.call(e, 70)
}
 
System.print("Ramanujan's constant to 32 decimal places is:")
System.print(ramanujan.call(163, 32).toDecimal(32))
var heegner = [19, 43, 67, 163]
System.print("\nHeegner numbers yielding almost integers:")
for (h in heegner) {
var r = ramanujan.call(h, 32)
var rc = r.ceil
var diff = (rc - r).toDecimal(32)
r = r.toDecimal(32)
rc = rc.toDecimal(32)
Fmt.print("$3d: $51s ≈ $18s (diff: $s)", h, r, rc, diff)
}</syntaxhighlight>
 
{{out}}
<pre>
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)
2,120

edits