Ramanujan's constant: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→Iterative calculations: cosmetic change only) |
|||
Line 311: | Line 311: | ||
</lang> |
</lang> |
||
=={{header|Nim}}== |
|||
{{libheader|nim-decimal}} |
|||
<lang Nim>import strformat, strutils |
|||
import decimal |
|||
setPrec(75) |
|||
let pi = newDecimal("3.1415926535897932384626433832795028841971693993751058209749445923078164") |
|||
proc eval(n: int): DecimalType = |
|||
result = exp(pi * sqrt(newDecimal(n))) |
|||
func format(n: DecimalType; d: Positive): string = |
|||
## Return the representation of "n" with "d" digits of precision. |
|||
let parts = ($n).split('.') |
|||
result = parts[0] & '.' & parts[1][0..<d] |
|||
echo "Ramanujan’s constant with 50 digits of precision:" |
|||
echo eval(163).format(50) |
|||
setPrec(50) |
|||
echo() |
|||
echo "Heegner numbers yielding 'almost' integers:" |
|||
for n in [19, 43, 67, 163]: |
|||
let x = eval(n) |
|||
let k = x.roundToInt |
|||
let d = x - k |
|||
let s = if d > 0: "+ " & $d else: "- " & $(-d) |
|||
echo &"{n:3}: {x}... = {k:>18} {s}..."</lang> |
|||
{{out}} |
|||
<pre>Ramanujan’s constant with 50 digits of precision: |
|||
262537412640768743.99999999999925007259719818568887935385633733699086 |
|||
Heegner numbers yielding 'almost' integers: |
|||
19: 885479.77768015431949753789348171962682071428650216... = 885480 - 0.22231984568050246210651828037317928571349784... |
|||
43: 884736743.99977746603490666193746207858537684739914... = 884736744 - 0.00022253396509333806253792141462315260086... |
|||
67: 147197952743.99999866245422450682926131257862850810... = 147197952744 - 0.00000133754577549317073868742137149190... |
|||
163: 262537412640768743.99999999999925007259719818568865... = 262537412640768744 - 7.4992740280181431135e-13...</pre> |
|||
=={{header|Pari/GP}}== |
=={{header|Pari/GP}}== |