I’m trying to do this Rosetta Code example, mimicking the Sidef
example.
# Sidef example and output
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))
})
# 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)
Crystal code and output.
def ramanujan_num(x, digits=32)
#(Math::E ** (Math::PI * Math.sqrt(x)) )
(Math.exp(Math::PI * Math.sqrt(x))).round(digits)
end
digits = 100
#puts "Ramanujan's constant to #{digits} decimals:\n #{ramanujan_num(163, digits)}"
#printf("Ramanujan's constant to #{digits} decimals:\n%s\n\n", ramanujan_num(163, digits))
printf("Ramanujan's constant to #{digits} decimals:\n%s\n\n", ramanujan_num(163, digits))
puts "Heegner numbers yielding 'almost' integers:"
[19, 96, 43, 960, 67, 5280, 163, 640320].each_slice(2) { |(h, x)|
c = ramanujan_num(h, 32)
n = (x**3 + 744)
printf("%3s: %51s ≈ %18s (diff: %s)\n", h, c, n, n-c)
}
Ramanujan's constant to 100 decimals:
2.6253741264076826e+17
Heegner numbers yielding 'almost' integers:
19: 885479.7776801554 ≈ 885480 (diff: 0.2223198446445167)
43: 884736743.9997752 ≈ 884736744 (diff: 0.00022482872009277344)
Unhandled exception: Arithmetic overflow (OverflowError)
from /home/jzakiya/crystal/share/crystal/src/int.cr:280:14 in '**'
from ramanujan.cr:16:8 in '__crystal_main'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:105:5 in 'main_user_code'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:91:7 in 'main'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:114:3 in 'main'
from __libc_start_main
from ../sysdeps/x86_64/start.S:122:0 in '_start'
from ???
}
I was looking at the docs to see if there’s a way to increase the computational precision.