Skip to content

Commit 22d960e

Browse files
committed
GH review comments
1 parent ef8dedd commit 22d960e

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ converted.pdf
88
/public
99
.firebase/
1010
firebase-debug.log
11-
.idea/
1211
__pycache__/
1312

1413
authors.json

src/others/pells_equation.md

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ Alternative formulation: We want to find all the possible solutions of the equat
1414
Here we will consider the case when $d$ is not a perfect square and $d>1$. The case when $d$ is a perfect square is trivial.
1515
We can even assume that $d$ is square-free (i.e. it is not divisible by the square of any prime number) as we can absorb the factors of $d$ into $y$.
1616

17-
$x^{2} - d \cdot y^{2} = ( x + \sqrt{d} \cdot y ) ( x - \sqrt{d} \cdot y ) = 1$
17+
We can rewrite the equation as:
1818

19-
The first part $( x + \sqrt{d} \cdot y )$ is always greater than 1. And the second part $( x - \sqrt{d} \cdot y )$ is always less than 1, since the product is 1.
19+
$$x^{2} - d y^{2} = (x + y \sqrt{d})(x - y \sqrt{d}) = 1$$
2020

21-
We will prove that all solutions to Pell's equation are given by powers of the smallest positive solution. Let's assume it to be
22-
$x_{0} + y_{0} \cdot \sqrt{d}$
21+
This factorization is important because it shows the connection to quadratic irrationals. The first part $(x + y \sqrt{d})$ is always greater than 1, and the second part $(x - y \sqrt{d})$ is always less than 1, since their product is 1.
2322

24-
We use method of descent to prove it.
23+
The norm of an expression $u + v \sqrt{d}$ is defined as $N(u + v \sqrt{d}) = (u + v \sqrt{d})(u - v \sqrt{d}) = u^2 - d v^2$. The norm is multiplicative: $N(ab) = N(a)N(b)$. This property is crucial in the descent argument below.
24+
25+
We will prove that all solutions to Pell's equation are given by powers of the smallest positive solution. Let's assume it to be $x_0 + y_0 \sqrt{d}$, where $x_0 > 1$ is the smallest possible value for $x$.
26+
27+
## Method of Descent
2528
Suppose there is a solution $u + v \cdot \sqrt{d}$ such that $u^{2} - d \cdot v^{2} = 1$ and is not a power of $( x_{0} + \sqrt{d} \cdot y_{0} )$
2629
Then it must lie between two powers of $( x_{0} + \sqrt{d} \cdot y_{0} )$.
2730
i.e, For some n, $( x_{0} + \sqrt{d} \cdot y_{0} )^{n} < u + v \cdot \sqrt{d} < ( x_{0} + \sqrt{d} \cdot y_{0} )^{n+1}$
@@ -36,40 +39,43 @@ Hence, we conclude that all solutions are given by $( x_{0} + \sqrt{d} \cdot y_{
3639

3740
## Finding the smallest positive solution
3841
### Expressing the solution in terms of continued fractions
39-
We can express the solution in terms of continued fractions. The continued fraction of $\sqrt{d}$ is periodic. Let's assume the continued fraction of $\sqrt{d}$ is $[a_{0}; \overline{a_{1}, a_{2}, \ldots, a_{r}}]$. The smallest positive solution is given by the convergent $[a_{0}; a_{1}, a_{2}, \ldots, a_{r}]$ where $r$ is the period of the continued fraction.
42+
We can express the solution in terms of continued fractions. The continued fraction of $\sqrt{d}$ is periodic. Let's assume the continued fraction of $\sqrt{d}$ is $[a_0; \overline{a_1, a_2, \ldots, a_r}]$. The smallest positive solution is given by the convergent $[a_0; a_1, a_2, \ldots, a_r]$ where $r$ is the period of the continued fraction.
4043

41-
The convergents $p_{n}/q_{n}$ are the rational approximations to $\sqrt{d}$ obtained by truncating the continued fraction expansion at each stage. These convergents can be computed recursively.
44+
The convergents $p_n/q_n$ are the rational approximations to $\sqrt{d}$ obtained by truncating the continued fraction expansion at each stage. These convergents can be computed recursively. For Pell's equation, the convergent $(p_n, q_n)$ at the end of the period solves $p_n^2 - d q_n^2 = \pm 1$.
4245

43-
Check whether the convergent satisfies the Pell's equation. If it does, then the convergent is the smallest positive solution.
46+
Check whether the convergent satisfies Pell's equation. If it does, then the convergent is the smallest positive solution.
4447

45-
Let's take an example to understand this by solving the equation $x^{2} - 2 \cdot y^{2} = 1$.
48+
Let's take an example to understand this by solving the equation $x^2 - 2 y^2 = 1$.
4649
$\sqrt{2} = [1; \overline{2}] = 1 + 1/(2 + 1/(2 + 1/(2+ ...)))$. The convergents are $1/1, 3/2, 7/5, 17/12, 41/29, 99/70, \ldots$.
47-
Now check for each convergent whether it satisfies the Pell's equation. The smallest positive solution is $3/2$.
48-
49-
### How to calculate the continued fraction of $\sqrt{d}$?
50-
Let's find the continued fraction of $\sqrt{7}$.
51-
52-
$\sqrt{7} \approx 2.6457 = 2 + 0.6457$
53-
54-
$a_{0} = 2$
55-
56-
Subtract $a_{0}$ from the number and take the reciprocal of the remaining number.
57-
58-
That is, we calculate ${1\over \sqrt{7} - 2} \approx 1.5486$. The integer part $a_{1}$ is $1$.
59-
So:
60-
$\sqrt{7}=2+\cfrac{1}{1+\cfrac1{\vdots}}$
61-
Where we haven't calculated the $( \vdots )$ part yet.
62-
To get that, we subtract $a_{1}$ from the number and take the reciprocal of the remaining number. That is, we calculate ${1\over 1.5486 - 1} \approx 1.8228$. The integer part $a_{2}$ is $1$.
63-
So:
64-
$\sqrt{7}=2+\cfrac{1}{1+\cfrac1{1+\cfrac1{\vdots}}}$
65-
Now ${1\over 1.8228 - 1} \approx 1.2153$. So $a_{3} = 1$.
66-
Continuing this process, ${1\over 1.2153 - 1} \approx 4.645$. So $a_{4} = 4$.
67-
68-
$$\sqrt{7}=2+\cfrac{1}{1+\cfrac1{1+\cfrac1{1+\cfrac4{\vdots}}}}$$
69-
70-
we get the continued fraction of $\sqrt{7}$ as $[2; 1, 1, 4, 1, 1, 4, \ldots]$.
50+
Now check for each convergent whether it satisfies Pell's equation. The smallest positive solution is $3/2$.
51+
52+
#### Integer-based continued fraction calculation
53+
For integer-based calculation, see the [Quadratic irrationality section of the continued fractions article](https://cp-algorithms.com/algebra/continued-fractions.html). Here is a sample algorithm in Python:
54+
55+
```python
56+
# Compute the continued fraction expansion of sqrt(n) using integer arithmetic
57+
import math
58+
59+
def continued_fraction_sqrt(n):
60+
m0 = 0
61+
d0 = 1
62+
a0 = int(math.isqrt(n))
63+
period = []
64+
m, d, a = m0, d0, a0
65+
seen = set()
66+
while (m, d, a) not in seen:
67+
seen.add((m, d, a))
68+
m = d * a - m
69+
d = (n - m * m) // d
70+
a = (a0 + m) // d
71+
period.append(a)
72+
return [a0] + period
73+
74+
# Example: sqrt(7)
75+
print(continued_fraction_sqrt(7)) # Output: [2, 1, 1, 1, 4]
76+
```
7177

72-
This can also be calculated using [integer based calculation(continued fractions)](https://cp-algorithms.com/algebra/continued-fractions.html)
78+
This method avoids floating-point errors and is suitable for large $d$.
7379

7480
### Finding the solution using Chakravala method
7581
The Chakravala method is an ancient Indian algorithm to solve Pell's equation. It is based on the Brahmagupta's identity of quadratic decomposition

0 commit comments

Comments
 (0)