51 lines
1.3 KiB
Plaintext
51 lines
1.3 KiB
Plaintext
digits = input("Enter number of digits to calculate after decimal point: ").val
|
|
// I've seen variations of this "precision" calculation from
|
|
// 10 * digits
|
|
// to
|
|
// floor(10 * digits / 3) + 16
|
|
// A larger value provides a more precise calculation but also
|
|
// takes longer to run. Based on my testing, this calculation
|
|
// below for precision produces accurate output for inputs
|
|
// from 1 to 4000 - haven't tried larger than this.
|
|
precision = floor(10 * digits / 3) + 4
|
|
A = [2] * precision
|
|
nines = 0
|
|
predigit = 0
|
|
cnt = 0
|
|
while cnt <= digits
|
|
carry = 0
|
|
for i in range(precision - 1, 1, -1)
|
|
temp = 10 * A[i] + carry * i
|
|
A[i] = temp % (2 * i - 1)
|
|
carry = floor(temp/(2 * i - 1))
|
|
end for
|
|
A[1] = carry % 10
|
|
carry = floor(carry / 10)
|
|
current = carry
|
|
if current == 9 then
|
|
nines += 1
|
|
else if current == 10 then
|
|
print (predigit+1), ""
|
|
cnt += 1
|
|
if nines > 0 then
|
|
print "9" * nines, ""
|
|
cnt += nines
|
|
end if
|
|
predigit = 0
|
|
nines = 0
|
|
else
|
|
// the first digit produced is always a zero
|
|
// don't need to see that
|
|
if cnt != 0 then print predigit, ""
|
|
cnt += 1
|
|
predigit = current
|
|
if nines > 0 then
|
|
print "9" * nines, ""
|
|
cnt += nines
|
|
end if
|
|
nines = 0
|
|
end if
|
|
if cnt == 2 then print ".", ""
|
|
end while
|
|
print str(predigit) * (cnt < digits + 2)
|