Python: Вычисление числа Пи
This commit is contained in:
parent
b880a2ba78
commit
5414fa8538
@ -5,3 +5,7 @@
|
||||
|
||||
## Stuff
|
||||
- [`irc-bot.py`](irc-bot.py) - Простой IRC бот на Python 3
|
||||
|
||||
## Tests
|
||||
|
||||
- [`picalc.py`](picalc.py) - Вычисления числа Пи
|
||||
|
40
snipplets/code/Python/picalc.py
Normal file
40
snipplets/code/Python/picalc.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user