Python: Вычисление числа Пи

This commit is contained in:
Alexander Popov 2023-09-20 20:00:00 +03:00
parent b880a2ba78
commit 5414fa8538
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
2 changed files with 44 additions and 0 deletions

View File

@ -5,3 +5,7 @@
## Stuff ## Stuff
- [`irc-bot.py`](irc-bot.py) - Простой IRC бот на Python 3 - [`irc-bot.py`](irc-bot.py) - Простой IRC бот на Python 3
## Tests
- [`picalc.py`](picalc.py) - Вычисления числа Пи

View File

@ -0,0 +1,40 @@
# 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)