From 5414fa8538cabd663da7351a566bec1b5356810f Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Wed, 20 Sep 2023 20:00:00 +0300 Subject: [PATCH] =?UTF-8?q?Python:=20=D0=92=D1=8B=D1=87=D0=B8=D1=81=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=87=D0=B8=D1=81=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=9F=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snipplets/code/Python/README.md | 4 ++++ snipplets/code/Python/picalc.py | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 snipplets/code/Python/picalc.py 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)