Ramanujan's constant: Difference between revisions
m
→{{header|Fōrmulæ}}
Thundergnat (talk | contribs) 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}}
'''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 583 ⟶ 593:
while (abs($mid - $exp) > ε) {
$sqr = sqrt($sqr);
if ($mid <= $exp) { $low = $mid; $acc
else { $high = $mid; $acc
$mid = ($low + $high) / 2;
}
Line 598 ⟶ 608:
for ^d {
given [ ($a + $g)/2, sqrt $a
$z -= (.[0] - $a)**2
$n += $n;
($a, $g) = @$_;
Line 609 ⟶ 619:
multi sqrt(FatRat $r --> FatRat) {
FatRat.new: sqrt($r.nude[0]
}
Line 624 ⟶ 634:
# calculation of 𝑒
sub postfix:<!> (Int $n) { (constant f = 1, |[\
sub 𝑒 (--> FatRat) { sum map { FatRat.new(1,.!) }, ^D }
Line 642 ⟶ 652:
constant 𝑒 = &𝑒();
my $Ramanujan = 𝑒**(π
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 = 𝑒**(π
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="
import "./fmt" for Fmt
var pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164"
|