snipplets.dev/code/Python/picalc.py

41 lines
907 B
Python
Raw Normal View History

# Source: https://stackoverflow.com/questions/9004789/1000-digits-of-pi-in-python
import time
def make_pi():
q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
for j in range(10000):
if 4 * q + r - t < m * t:
yield m
q, r, t, k, m, x = (
10 * q,
10 * (r - m * t),
t,
k,
(10 * (3 * q + r)) // t - 10 * m,
x,
)
else:
q, r, t, k, m, x = (
q * k,
(2 * q + r) * x,
t * x,
k + 1,
(q * (7 * k + 2) + r * x) // (t * x),
x + 2,
)
t1 = time.time()
pi_array = []
for i in make_pi():
pi_array.append(str(i))
pi_array = pi_array[:1] + ['.'] + pi_array[1:]
pi_array_str = ''.join(pi_array)
print('PI:', pi_array_str)
print('dT:', time.time() - t1)