Ramanujan's constant: Difference between revisions
m
→{{header|Fōrmulæ}}
(Initial Haskell version.) |
|||
(7 intermediate revisions by 5 users not shown) | |||
Line 8:
=={{header|C++}}==
{{libheader|Boost}}
<
#include <iostream>
#include <boost/math/constants/constants.hpp>
Line 33:
}
return 0;
}</
{{out}}
Line 49:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Ramanujan%27s_constant}}
'''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]]
[[File:Fōrmulæ - Ramanujan's constant 04.png]]
=={{header|Go}}==
Line 60 ⟶ 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.
<
import (
Line 99 ⟶ 109:
fmt.Printf("%3d: %51.32f ≈ %18d (diff: %.32f)\n", h[0], qh, c, t)
}
}</
{{out}}
Line 114 ⟶ 124:
=={{header|Haskell}}==
Calculations are done using an arbitrary precision "constructive reals" type (CReal).
<
import Data.Number.CReal (CReal, showCReal)
import Text.Printf (printf)
Line 141 ⟶ 152:
let r = ramfun (fromIntegral h)
d = intDist r
in printf "%3d %54s %s\n" h (showCReal n r) (showCReal 15 d)</
{{out}}
Line 229 ⟶ 240:
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
Line 238 ⟶ 249:
262537412640768743.9999999999992500711164316586918409066184
NB. ^
</syntaxhighlight>
=={{header|Java}}==
Very interesting. Compute Pi, E, and square root to arbitrary precision.
<
import java.math.BigDecimal;
import java.math.MathContext;
Line 330 ⟶ 341:
}
</syntaxhighlight>
{{out}}
Line 344 ⟶ 355:
=={{header|Julia}}==
<
julia> a = BigFloat(MathConstants.e^(BigFloat(pi)))^(BigFloat(163.0)^0.5)
Line 352 ⟶ 363:
7.499274028018143111206461436626630091372924626572825942241598957614307213309258e-13
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Table[
c = N[Exp[Pi Sqrt[h]], 40];
Line 361 ⟶ 372:
,
{h, {19, 43, 67, 163}}
]</
{{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}
Line 370 ⟶ 381:
=={{header|Nim}}==
{{libheader|nim-decimal}}
<
import decimal
Line 396 ⟶ 407:
let d = x - k
let s = if d > 0: "+ " & $d else: "- " & $(-d)
echo &"{n:3}: {x}... = {k:>18} {s}..."</
{{out}}
Line 409 ⟶ 420:
=={{header|Pari/GP}}==
<
exp(Pi*sqrt(163))</
{{out}}
<pre>
Line 419 ⟶ 430:
===Direct calculation===
{{trans|Sidef}}
<
use warnings;
use Math::AnyNum;
Line 445 ⟶ 456:
my $n = Math::AnyNum::ipow($x, 3) + 744;
printf("%3s: %51s ≈ %18s (diff: %s)\n", $h, $c, $n, ($n - $c)->round(-32));
}</
{{out}}
<pre>
Line 460 ⟶ 471:
===Continued fractions===
{{trans|Raku}}
<
use Math::AnyNum <as_dec rat>;
Line 474 ⟶ 485:
printf "Ramanujan's constant\n%s\n", as_dec($R,58);
</syntaxhighlight>
{{out}}
<pre>Ramanujan's constant
Line 482 ⟶ 493:
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<
<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>
Line 520 ⟶ 531:
<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>
<!--</
{{out}}
<pre>
Line 535 ⟶ 546:
=={{header|Python}}==
{{libheader|mpmath}}
<
heegner = [19,43,67,163]
mp.dps = 50
Line 543 ⟶ 554:
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>
Line 562 ⟶ 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"
# set the degree of precision for calculations
Line 582 ⟶ 593:
while (abs($mid - $exp) > ε) {
$sqr = sqrt($sqr);
if ($mid <= $exp) { $low = $mid; $acc
else { $high = $mid; $acc
$mid = ($low + $high) / 2;
}
Line 597 ⟶ 608:
for ^d {
given [ ($a + $g)/2, sqrt $a
$z -= (.[0] - $a)**2
$n += $n;
($a, $g) = @$_;
Line 608 ⟶ 619:
multi sqrt(FatRat $r --> FatRat) {
FatRat.new: sqrt($r.nude[0]
}
Line 623 ⟶ 634:
# calculation of 𝑒
sub postfix:<!> (Int $n) { (constant f = 1, |[\
sub 𝑒 (--> FatRat) { sum map { FatRat.new(1,.!) }, ^D }
Line 641 ⟶ 652:
constant 𝑒 = &𝑒();
my $Ramanujan = 𝑒**(π
say "Ramanujan's constant to 32 decimal places:\nActual: " ~
"262537412640768743.99999999999925007259719818568888\n" ~
Line 648 ⟶ 659:
say "Heegner numbers yielding 'almost' integers";
for 19, 96, 43, 960, 67, 5280, 163, 640320 -> $heegner, $x {
my $almost = 𝑒**(π
my $exact = $x³ + 744;
say format($exact, $almost);
}</
{{out}}
<pre>Ramanujan's constant to 32 decimal places:
Line 676 ⟶ 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"
sub continued-fraction($n, :@a, :@b) {
Line 688 ⟶ 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);</
{{out}}
<pre>Ramanujan's constant to 32 decimal places:
Line 695 ⟶ 706:
=={{header|REXX}}==
Instead of calculating <big> '''e''' </big> and <big><big><math>\pi</math></big></big> 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.
<
d= min( length(pi()), length(e()) ) - length(.) /*calculate max #decimal digs supported*/
parse arg digs sDigs . 1 . . $ /*obtain optional arguments from the CL*/
Line 729 ⟶ 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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 749 ⟶ 760:
=={{header|Ruby}}==
<
include BigMath
Line 756 ⟶ 767:
puts "#{x}: #{(e ** (pi * BigMath.sqrt(BigDecimal(x), 200))).round(100).to_s("F")}"
end
</syntaxhighlight>
{{out}}
<pre>19: 885479.7776801543194975378934817196268207142865018553571526577110128809842286637202423189990118182067775711
Line 764 ⟶ 775:
</pre>
=={{header|Sidef}}==
<
local Num!PREC = *"#{4*round((Num.pi*√x)/log(10) + decimals + 1)}"
exp(Num.pi * √x) -> round(-decimals).to_s
Line 778 ⟶ 789:
var n = (x**3 + 744)
printf("%3s: %51s ≈ %18s (diff: %s)\n", h, c, n, n-Num(c))
})</
{{out}}
<pre>
Line 797 ⟶ 808:
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.
<
import "./fmt" for Fmt
var pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164"
Line 835 ⟶ 846:
rc = rc.toDecimal(32)
Fmt.print("$3d: $51s ≈ $18s (diff: $s)", h, r, rc, diff)
}</
{{out}}
|