Advertisement
Guest User

fruit_math_solver.ipynb

a guest
Mar 30th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.17 KB | None | 0 0
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": 1,
  6.    "metadata": {},
  7.    "outputs": [
  8.     {
  9.      "name": "stdout",
  10.      "output_type": "stream",
  11.      "text": [
  12.       "Elliptic Curve defined by y^2 = x^3 + 109*x^2 + 224*x over Rational Field\n"
  13.      ]
  14.     }
  15.    ],
  16.    "source": [
  17.     "# From:\n",
  18.     "# Bremner, A., & MacLeod, A.J. (2014). An unusual cubic representation problem.\n",
  19.     "# http://publikacio.uni-eszterhazy.hu/2858/1/AMI_43_from29to41.pdf\n",
  20.     "\n",
  21.     "# Interested in solutions to (1.1):\n",
  22.     "#   a       b       c  \n",
  23.     "# ----- + ----- + ----- = n\n",
  24.     "# b + c   a + c   a + b\n",
  25.     "\n",
  26.     "n = 4\n",
  27.     "assert (n % 2 == 0)\n",
  28.     "\n",
  29.     "# Homogeneity gives the following cubic \n",
  30.     "# C_N : N(a + b)(b + c)(c + a) = a(a + b)(c + a) + b(b + c)(a + b) + c(c + a)(b + c)\n",
  31.     "# in 2 dimensional projective space\n",
  32.     "\n",
  33.     "# And we get the following elliptic\n",
  34.     "# E_N : y^2 = x^3 + (4N^2 + 12N − 3)x^2 + 32(N + 3)x\n",
  35.     "\n",
  36.     "E = EllipticCurve([0, ((4 * n ** 2) + (12 * n) - 3), 0, (32 * (n + 3)), 0]); print(E)\n",
  37.     "\n",
  38.     "# The accompanying morphisms\n",
  39.     "# Projective cubic to elliptic \n",
  40.     "def cubic_to_elliptic(a, b, c):\n",
  41.     "    x = (-4 * (a + b + 2*c) * (n + 3))/((2 * a + 2 * b - c) + n * (a + b))\n",
  42.     "    y = (4 * (a - b) * (n + 3) * (2 * n + 5))/((2 * a + 2 * b - c) + n * (a + b))\n",
  43.     "    return x, y\n",
  44.     "\n",
  45.     "# The inverse\n",
  46.     "def elliptic_to_cubic(x, y):\n",
  47.     "    a = (8 * (n + 3) - x + y)/(2 * (4 - x) * (n + 3))\n",
  48.     "    b = (8 * (n + 3) - x - y)/(2 * (4 - x) * (n + 3))\n",
  49.     "    c = (-4 * (n + 3) - x * (n + 2))/((4 - x) * (n + 3))\n",
  50.     "    return a, b, c"
  51.    ]
  52.   },
  53.   {
  54.    "cell_type": "code",
  55.    "execution_count": 2,
  56.    "metadata": {},
  57.    "outputs": [
  58.     {
  59.      "name": "stdout",
  60.      "output_type": "stream",
  61.      "text": [
  62.       "(-100 : 260 : 1)\n",
  63.       "Torsion Subgroup isomorphic to Z/6 associated to the Elliptic Curve defined by y^2 = x^3 + 109*x^2 + 224*x over Rational Field\n",
  64.       "(0 : 1 : 0)\n",
  65.       "(56 : 728 : 1)\n",
  66.       "(4 : 52 : 1)\n",
  67.       "(0 : 0 : 1)\n",
  68.       "(4 : -52 : 1)\n",
  69.       "(56 : -728 : 1)\n"
  70.      ]
  71.     }
  72.    ],
  73.    "source": [
  74.     "# Get generator point for finding solutions\n",
  75.     "try:\n",
  76.     "    Q = E.gen(0); print(Q)\n",
  77.     "except:\n",
  78.     "    # Just in case we have a bad n \n",
  79.     "    print(f\"Error!\\nNo solution exists for n = {n}\")\n",
  80.     "    \n",
  81.     "# Torsion subgroup always isomorphic to Z/Z6 by Lemma 2.1\n",
  82.     "G = E.torsion_subgroup(); print(G);\n",
  83.     "\n",
  84.     "for t in G:\n",
  85.     "    print(t)"
  86.    ]
  87.   },
  88.   {
  89.    "cell_type": "code",
  90.    "execution_count": 3,
  91.    "metadata": {},
  92.    "outputs": [],
  93.    "source": [
  94.     "# Condition on the x coordinate of the elliptic for a \n",
  95.     "# solution to be positive in the cubic, by Theorem 4.1\n",
  96.     "\n",
  97.     "# There is a minor error in the figure (4.1), the numerator \n",
  98.     "# of the lower bound is missing a parenthesis on the right\n",
  99.     "\n",
  100.     "lbound1 = (3 - 12 * n - 4 * n ** 2 - ((2 * n + 5) * sqrt(4 * n ** 2 + 4 * n - 15))) / 2\n",
  101.     "ubound1 = -2 * (n + 3) * (n + sqrt(n ** 2 - 4))\n",
  102.     "\n",
  103.     "lbound2 = -2 * (n + 3) * (n - sqrt(n ** 2 - 4))\n",
  104.     "ubound2 = -4 * ((n + 3) / (n + 2))\n",
  105.     "\n",
  106.     "def positive_solution(pt):\n",
  107.     "    x = pt[0]          \n",
  108.     "    if (x > lbound1 and x < ubound1):\n",
  109.     "        return True\n",
  110.     "    if (x > lbound2 and x < ubound2):\n",
  111.     "        return True\n",
  112.     "    return False\n",
  113.     "\n",
  114.     "\n",
  115.     "def find_solution():\n",
  116.     "    temp = Q\n",
  117.     "    m = 0;\n",
  118.     "    while True:\n",
  119.     "        # Take our current point, iQ, try torsion\n",
  120.     "        for t in G:\n",
  121.     "            temp_tor = temp + E(t)\n",
  122.     "\n",
  123.     "            if positive_solution(temp_tor):\n",
  124.     "                return temp_tor, m, t\n",
  125.     "\n",
  126.     "        m += 1\n",
  127.     "        temp += Q\n",
  128.     "\n",
  129.     "ell_solution, m, t = find_solution()"
  130.    ]
  131.   },
  132.   {
  133.    "cell_type": "code",
  134.    "execution_count": 4,
  135.    "metadata": {
  136.     "scrolled": true
  137.    },
  138.    "outputs": [
  139.     {
  140.      "name": "stdout",
  141.      "output_type": "stream",
  142.      "text": [
  143.       "Found solution with max length: 81\n",
  144.       "Solution = 8Q + (0 : 1 : 0) \n",
  145.       "a=154476802108746166441951315019919837485664325669565431700026634898253202035277999\n",
  146.       "b=36875131794129999827197811565225474825492979968971970996283137471637224634055579\n",
  147.       "c=4373612677928697257861252602371390152816537558161613618621437993378423467772036\n",
  148.       "\n"
  149.      ]
  150.     }
  151.    ],
  152.    "source": [
  153.     "# Get (projective) cubic points\n",
  154.     "solution = elliptic_to_cubic(ell_solution[0], ell_solution[1])\n",
  155.     "\n",
  156.     "# LCM of denominators to convert to integers\n",
  157.     "scale_factor = lcm([solution[0].denom(), solution[1].denom(), solution[2].denom()])\n",
  158.     "\n",
  159.     "# Integer solution\n",
  160.     "a, b, c = solution[0] * scale_factor, solution[1] * scale_factor, solution[2] * scale_factor\n",
  161.     "solution = [a, b, c]\n",
  162.     "\n",
  163.     "# Sanity\n",
  164.     "assert(all(int(x) == x for x in solution))\n",
  165.     "assert(all(x > 0 for x in solution))\n",
  166.     "\n",
  167.     "sol_len = max(len(str(a)), len(str(b)), len(str(c)))\n",
  168.     "\n",
  169.     "print('Found solution with max length:', sol_len)\n",
  170.     "\n",
  171.     "# Compare to Table 2\n",
  172.     "print(f'Solution = {m}Q + {t} ')\n",
  173.     "\n",
  174.     "# Printing the solutions\n",
  175.     "print(f'a={a}\\nb={b}\\nc={c}\\n')"
  176.    ]
  177.   }
  178.  ],
  179.  "metadata": {
  180.   "kernelspec": {
  181.    "display_name": "SageMath 9.1",
  182.    "language": "sage",
  183.    "name": "sagemath"
  184.   },
  185.   "language_info": {
  186.    "codemirror_mode": {
  187.     "name": "ipython",
  188.     "version": 3
  189.    },
  190.    "file_extension": ".py",
  191.    "mimetype": "text/x-python",
  192.    "name": "python",
  193.    "nbconvert_exporter": "python",
  194.    "pygments_lexer": "ipython3",
  195.    "version": "3.7.3"
  196.   },
  197.   "widgets": {
  198.    "application/vnd.jupyter.widget-state+json": {
  199.     "state": {},
  200.     "version_major": 2,
  201.     "version_minor": 0
  202.    }
  203.   }
  204.  },
  205.  "nbformat": 4,
  206.  "nbformat_minor": 2
  207. }
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement