RosettaCodeData/Task/Pi/Sidef/pi-1.sidef

27 lines
599 B
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

func pi(callback) {
var (q, r, t, k, n, l) = (1, 0, 1, 1, 3, 3)
loop {
if ((4*q + r - t) < n*t) {
callback(n)
static _dot = callback('.')
var nr = 10*(r - n*t)
n = ((10*(3*q + r)) // t - 10*n)
q *= 10
r = nr
}
else {
var nr = ((2*q + r) * l)
var nn = ((q*(7*k + 2) + r*l) // (t*l))
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
}
}
}
 
STDOUT.autoflush(true)
pi(func(digit){ print digit })