diff --git a/snipplets/code/Python/README.md b/snipplets/code/Python/README.md index 65f6f12..736d116 100644 --- a/snipplets/code/Python/README.md +++ b/snipplets/code/Python/README.md @@ -5,3 +5,7 @@ ## Stuff - [`irc-bot.py`](irc-bot.py) - Простой IRC бот на Python 3 + +## Tests + +- [`picalc.py`](picalc.py) - Вычисления числа Пи diff --git a/snipplets/code/Python/picalc.py b/snipplets/code/Python/picalc.py new file mode 100644 index 0000000..389001b --- /dev/null +++ b/snipplets/code/Python/picalc.py @@ -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)