Ramanujan's constant: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(5 intermediate revisions by 3 users not shown)
Line 49:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Ramanujan%27s_constant}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. 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 storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Case 1. Calculate Ramanujan's constant with at least 32 digits of precision'''
In '''[https://formulae.org/?example=Ramanujan%27s_constant this]''' page you can see the program(s) related to this task and their results.
 
[[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 583 ⟶ 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 598 ⟶ 608:
 
for ^d {
given [ ($a + $g)/2, sqrt $a *× $g ] {
$z -= (.[0] - $a)**2 *× $n;
$n += $n;
($a, $g) = @$_;
Line 609 ⟶ 619:
 
multi sqrt(FatRat $r --> FatRat) {
FatRat.new: sqrt($r.nude[0] *× 10**(D*2D×2) div $r.nude[1]), 10**D;
}
 
Line 624 ⟶ 634:
 
# calculation of 𝑒
sub postfix:<!> (Int $n) { (constant f = 1, |[\*×] 1..*)[$n] }
sub 𝑒 (--> FatRat) { sum map { FatRat.new(1,.!) }, ^D }
 
Line 642 ⟶ 652:
constant 𝑒 = &𝑒();
 
my $Ramanujan = 𝑒**(π* × √163);
say "Ramanujan's constant to 32 decimal places:\nActual: " ~
"262537412640768743.99999999999925007259719818568888\n" ~
Line 649 ⟶ 659:
say "Heegner numbers yielding 'almost' integers";
for 19, 96, 43, 960, 67, 5280, 163, 640320 -> $heegner, $x {
my $almost = 𝑒**(π* × √$heegner);
my $exact = $x³ + 744;
say format($exact, $almost);
Line 798 ⟶ 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.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigRat
import "./fmt" for Fmt
 
var pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164"
2,120

edits