Pi: Difference between revisions

876 bytes added ,  3 years ago
m (Removed useless "import strutils" and useless variable "dd". Restructured some expressions.)
Line 3,632:
i = 0
eliminateDigit d</lang>
 
{{out}}
<pre>3141592653589793238462643383279502884197
1693993751058209749445923078164062862089
9862803482534211706798214808651328230664
7093844609550582231725359408128481117450
...</pre>
 
Another version which avoids to access the internals of big integers:
{{trans|D}}
{{libheader|bignum}}
<lang Nim>import bignum
 
proc calcPi() =
var
q = newInt(1)
r = newInt(0)
t = newInt(1)
k = newInt(1)
n = newInt(3)
l = newInt(3)
 
var count = 0
while true:
if 4 * q + r - t < n * t:
stdout.write n
inc count
if count == 40: (echo ""; count = 0)
let nr = 10 * (r - n * t)
n = 10 * (3 * q + r) div t - 10 * n
q *= 10
r = nr
else:
let nr = (2 * q + r) * l
let nn = (7 * q * k + 2 + r * l) div (t * l)
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
 
calcPi()</lang>
 
{{out}}
Anonymous user