refer to relevant problems instead of re-implementing

This commit is contained in:
filifa
2025-05-15 23:30:57 -04:00
parent 275bfae648
commit fc7c74c323
2 changed files with 34 additions and 58 deletions

View File

@@ -19,10 +19,10 @@
{
"data": {
"text/plain": [
"[(-sqrt(2)*(2*sqrt(2) + 3)^t + sqrt(2)*(-2*sqrt(2) + 3)^t - 3/2*(2*sqrt(2) + 3)^t - 3/2*(-2*sqrt(2) + 3)^t,\n",
" 3/4*sqrt(2)*(2*sqrt(2) + 3)^t - 3/4*sqrt(2)*(-2*sqrt(2) + 3)^t + (2*sqrt(2) + 3)^t + (-2*sqrt(2) + 3)^t),\n",
" (sqrt(2)*(2*sqrt(2) + 3)^t - sqrt(2)*(-2*sqrt(2) + 3)^t + 3/2*(2*sqrt(2) + 3)^t + 3/2*(-2*sqrt(2) + 3)^t,\n",
" -3/4*sqrt(2)*(2*sqrt(2) + 3)^t + 3/4*sqrt(2)*(-2*sqrt(2) + 3)^t - (2*sqrt(2) + 3)^t - (-2*sqrt(2) + 3)^t)]"
"[(sqrt(2)*(2*sqrt(2) + 3)^t - sqrt(2)*(-2*sqrt(2) + 3)^t + 3/2*(2*sqrt(2) + 3)^t + 3/2*(-2*sqrt(2) + 3)^t,\n",
" -3/4*sqrt(2)*(2*sqrt(2) + 3)^t + 3/4*sqrt(2)*(-2*sqrt(2) + 3)^t - (2*sqrt(2) + 3)^t - (-2*sqrt(2) + 3)^t),\n",
" (-sqrt(2)*(2*sqrt(2) + 3)^t + sqrt(2)*(-2*sqrt(2) + 3)^t - 3/2*(2*sqrt(2) + 3)^t - 3/2*(-2*sqrt(2) + 3)^t,\n",
" 3/4*sqrt(2)*(2*sqrt(2) + 3)^t - 3/4*sqrt(2)*(-2*sqrt(2) + 3)^t + (2*sqrt(2) + 3)^t + (-2*sqrt(2) + 3)^t)]"
]
},
"execution_count": 1,
@@ -97,51 +97,20 @@
"## Solving Pell equations\n",
"Lagrange proved that if $(x_0, y_0)$ is a solution to\n",
"$$x^2 - dy^2 = 1$$\n",
"then $\\frac{x_0}{y_0}$ is a [convergent of the continued fraction](https://en.wikipedia.org/wiki/Simple_continued_fraction) of $\\sqrt{d}$. This is great for us, since we can write generators for computing convergents of square roots (FYI, SageMath can do this with built-in methods: `continued_fraction(sqrt(d)).convergents()`)."
"then $\\frac{x_0}{y_0}$ is a [convergent of the continued fraction](https://en.wikipedia.org/wiki/Simple_continued_fraction) of $\\sqrt{d}$. This is great for us, since there are algorithms to compute these convergents. We'll use SageMath here; see [problem 64](https://projecteuler.net/problem=64) for how to compute the partial denominators of the continued fraction of a square root, and see [problem 65](https://projecteuler.net/problem=65) for an algorithm that uses partial denominators to compute convergents of continued fractions.\n",
"\n",
"Here, we iterate over each convergent to see if its numerator and denominator are a solution to the Pell equation."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c51f9b7e",
"metadata": {},
"outputs": [],
"source": [
"def continued_fraction_sqrt(d):\n",
" x = sqrt(d)\n",
" while True:\n",
" b = floor(x)\n",
" yield b\n",
" x = (x - b)^-1\n",
" \n",
" \n",
"def convergents(partial_denoms):\n",
" h, hprev = 1, 0\n",
" k, kprev = 0, 1\n",
" for b in partial_denoms:\n",
" h, hprev = b * h + hprev, h\n",
" k, kprev = b * k + kprev, k\n",
" yield h/k"
]
},
{
"cell_type": "markdown",
"id": "d5cbf5eb",
"metadata": {},
"source": [
"Then we can just iterate over each convergent to see if its numerator and denominator are a solution to the Pell equation."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5d95125c",
"metadata": {},
"outputs": [],
"source": [
"def pell_fundamental_solution(d):\n",
" partial_denoms = continued_fraction_sqrt(d)\n",
" for f in convergents(partial_denoms):\n",
" for f in continued_fraction(sqrt(d)).convergents():\n",
" x, y = f.as_integer_ratio()\n",
" if x^2 - d*y^2 == 1:\n",
" return (x, y)"
@@ -157,7 +126,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"id": "03d7ba9d",
"metadata": {},
"outputs": [
@@ -167,7 +136,7 @@
"661"
]
},
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}