Jump to content

Ramanujan's constant: Difference between revisions

Initial Haskell version.
(Initial Haskell version.)
Line 111:
67: 147197952743.99999866245422450682926131257863 ≈ 147197952744 (diff: 0.00000133754577549317073868742137)
163: 262537412640768743.99999999999925007259719818568888 ≈ 262537412640768744 (diff: 0.00000000000074992740280181431112)
</pre>
 
=={{header|Haskell}}==
<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)</lang>
 
{{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>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.