summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Guschin <guschin.drew@gmail.com>2023-12-18 21:48:34 +0400
committerAndrew Guschin <guschin.drew@gmail.com>2023-12-18 21:48:34 +0400
commitb6eb4486c85244809765b02ab313804f14684355 (patch)
treed46a43262c030be7e236396a8066c8652f9146a8
задание 1
-rw-r--r--.gitignore2
-rw-r--r--t4mk.ipynb242
2 files changed, 244 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a40f1fd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.*
+!.gitignore \ No newline at end of file
diff --git a/t4mk.ipynb b/t4mk.ipynb
new file mode 100644
index 0000000..1f12fc8
--- /dev/null
+++ b/t4mk.ipynb
@@ -0,0 +1,242 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Задание 1\n",
+ "\n",
+ "Изменяем значение `frac` на свою дробь."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 41,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/latex": [
+ "$\\displaystyle \\Large\\frac{157}{225}$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Math object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "data": {
+ "text/latex": [
+ "$\\displaystyle 0 + \\Large\\frac{1}{\\Large\\frac{225}{157}}$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Math object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "data": {
+ "text/latex": [
+ "$\\displaystyle 0 + \\Large\\frac{1}{1 + \\Large\\frac{1}{\\Large\\frac{157}{68}}}$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Math object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "data": {
+ "text/latex": [
+ "$\\displaystyle 0 + \\Large\\frac{1}{1 + \\Large\\frac{1}{2 + \\Large\\frac{1}{\\Large\\frac{68}{21}}}}$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Math object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "data": {
+ "text/latex": [
+ "$\\displaystyle 0 + \\Large\\frac{1}{1 + \\Large\\frac{1}{2 + \\Large\\frac{1}{3 + \\Large\\frac{1}{\\Large\\frac{21}{5}}}}}$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Math object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "data": {
+ "text/latex": [
+ "$\\displaystyle 0 + \\Large\\frac{1}{1 + \\Large\\frac{1}{2 + \\Large\\frac{1}{3 + \\Large\\frac{1}{4 + \\Large\\frac{1}{5}}}}}$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Math object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "data": {
+ "text/latex": [
+ "$\\displaystyle \\begin{array}{ccccccccc}\n",
+ "i & -1 & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\\\\n",
+ "q_i & & & 0 & 1 & 2 & 3 & 4 & 5 \\\\\n",
+ "P_i & 0 & 1 & 0 & 1 & 2 & 7 & 30 & 157 \\\\\n",
+ "Q_i & 1 & 0 & 1 & 1 & 3 & 10 & 43 & 225 \\\\\n",
+ "\\end{array}$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Math object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "frac = (157, 225)\n",
+ "\n",
+ "from IPython.display import display, Math\n",
+ "\n",
+ "# Задание 1 - 157 225\n",
+ "def continued_fraction(a0, a1):\n",
+ " if a1 > 0:\n",
+ " qs = []\n",
+ " while a1 != 0:\n",
+ " r = a0 % a1\n",
+ " qs.append((a0 - r) // a1)\n",
+ " # print(f'{a0} / {a1} = {qs[-1]} + {a1} / {r}')\n",
+ " a0 = a1\n",
+ " a1 = r\n",
+ " # print('Непрерывная дробь', qs)\n",
+ " return qs\n",
+ " else:\n",
+ " return 'Знаменатель должен быть больше нуля!'\n",
+ "\n",
+ "\n",
+ "def successive_convergents(a0, a1):\n",
+ " qs = [0, 0] + continued_fraction(a0, a1)\n",
+ " big_ps = [0, 1]\n",
+ " big_qs = [1, 0]\n",
+ " for i in range(2, len(qs)):\n",
+ " big_ps.append(big_ps[i - 1] * qs[i] + big_ps[i - 2])\n",
+ " big_qs.append(big_qs[i - 1] * qs[i] + big_qs[i - 2])\n",
+ " # print(f'P = {big_ps}')\n",
+ " # print(f'Q = {big_qs}')\n",
+ " qs[0] = qs[1] = None\n",
+ " return qs, big_ps, big_qs\n",
+ "\n",
+ "\n",
+ "class Num:\n",
+ " def __init__(self, val):\n",
+ " self.val = val\n",
+ "\n",
+ " def math(self):\n",
+ " return str(self.val)\n",
+ "\n",
+ "class Fr:\n",
+ " def __init__(self, den, num):\n",
+ " self.val = None\n",
+ " self.den = den\n",
+ " self.num = num\n",
+ " \n",
+ " def math(self):\n",
+ " if self.val is None:\n",
+ " return f\"\\\\Large\\\\frac{{{self.den}}}{{{self.num}}}\"\n",
+ " else:\n",
+ " return f\"{self.val} + \\\\Large\\\\frac{{{self.den}}}{{{self.num.math()}}}\"\n",
+ " \n",
+ " def display(self):\n",
+ " return display(Math(self.math()))\n",
+ " \n",
+ " def next(self):\n",
+ " ret = True\n",
+ " if self.val is None:\n",
+ " self.val = self.den // self.num\n",
+ " self.den = self.den % self.num\n",
+ " if self.den == 1:\n",
+ " f1 = Num(self.num)\n",
+ " ret = False\n",
+ " else:\n",
+ " f1 = Fr(self.num, self.den)\n",
+ " self.den = 1\n",
+ " self.num = f1\n",
+ " else:\n",
+ " ret = self.num.next()\n",
+ " return ret\n",
+ "\n",
+ "\n",
+ "def display_table(table):\n",
+ " qs, bps, bqs = table\n",
+ " res = [r\"\\begin{array}{\" + 'c' * (len(qs) + 1) + '}']\n",
+ "\n",
+ " i_row = ['i'] + list(map(str, range(-1, len(qs) - 2 + 1)))\n",
+ " q_row = ['q_i', '', '']\n",
+ " bq_row = ['Q_i']\n",
+ " bp_row = ['P_i']\n",
+ " for (qi, bqi, bpi) in zip(qs, bqs, bps):\n",
+ " if qi is not None:\n",
+ " q_row.append(str(qi))\n",
+ " bq_row.append(str(bqi))\n",
+ " bp_row.append(str(bpi))\n",
+ " res.append(' & '.join(i_row) + r' \\\\')\n",
+ " res.append(' & '.join(q_row) + r' \\\\')\n",
+ " res.append(' & '.join(bp_row) + r' \\\\')\n",
+ " res.append(' & '.join(bq_row) + r' \\\\')\n",
+ "\n",
+ " res.append(r\"\\end{array}\")\n",
+ " res = '\\n'.join(res)\n",
+ " return display(Math(res))\n",
+ "\n",
+ "f = Fr(*frac)\n",
+ "while True:\n",
+ " f.display()\n",
+ " if not f.next():\n",
+ " break\n",
+ "f.display()\n",
+ "\n",
+ "table = successive_convergents(*frac)\n",
+ "display_table(table)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Задание 2"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.11.6"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}