Quadratic Diophantine equations that I often heard of but didn't understand

Правка en12, от adamant, 2023-05-08 00:45:14

Hi everyone!

Recently I started solving projecteuler, and while doing so I encountered two concepts about which I heard before, but I didn't really bother to learn them. I made a few notes to myself about how they work, and thought it could be useful for somebody else too. This blog focuses on Pythagorean triples and Pell's equations, which are recurrent concepts on projecteuler.

Great thanks to nor, Endagorion, Golovanov399 and Neodym for useful discussions about these topics.


Pythagorean triples

Pythagorean triple is a triple $$$a,b,c \in \mathbb Z$$$ such that

$$$ a^2 + b^2 = c^2. $$$

It is possible to parameterize them in a way that allows to find all triples such that $$$x,y,z \leq n$$$ in $$$O(\sqrt n)$$$.

Rational points on a unit circle

To do that, let's divide the equation by $$$z^2$$$ to get

$$$ \left(\frac{a}{c}\right)^2 + \left(\frac{b}{c}\right)^2=1. $$$

This reduces the task of finding Pythagorean triples to finding rational points $$$(x, y)$$$ on a unit circle.

And there is a nice and simple way to find all such points. To do this, we should notice that a ray going from $$$(0, -1)$$$ into a rational point $$$(x, y)$$$ will intersect the line $$$y=0$$$ in a rational point $$$(\frac{p}{q}, 0)$$$. So, we can find all rational points $$$(x, y)$$$ by projecting the point $$$(\frac{p}{q}, 0)$$$ back onto unit circle for each $$$\frac{p}{q}$$$.


We consider a ray from $$$(0, -1)$$$ into $$$(\frac{p}{q}, 0)$$$ to find rational points on the unit circle

The direction vector of a ray from $$$(0, -1)$$$ to $$$(\frac{p}{q}, 0)$$$ is $$$(\frac{p}{q},1)$$$, so the equation of the ray is $$$py - xq = -p$$$. Let's solve the system

$$$\begin{cases} x^2 + y^2 = 1, \\ py - xq = -p. \end{cases}$$$

After expressing $$$y$$$ as a function of $$$x$$$ like

$$$ y = \frac{xq-p}{p}, $$$

we put it back into the first equation and get the result:

$$$\boxed{\begin{gather} x= \frac{2qp}{p^2+q^2}, & y = \frac{p^2-q^2}{p^2 + q^2} \end{gather}}$$$

As it turns out, the projection point on a unit circle is also always rational, so we found a bijection between rational points in $$$\mathbb R$$$ and rational points on the unit circle.

Back to integers

This translates back into integer triples as

$$$\boxed{\begin{gather} a = 2pq, & b = p^2 - q^2,& c = p^2 + q^2 \end{gather}}$$$

Note that for integers such parameterization is incomplete, as the fraction $$$x=\frac{a}{c}$$$ could as well correspond to $$$\frac{ka}{kc}$$$ for some integer $$$k$$$. Therefore, all possible triplets are obtained by multiplying vectors $$$(a,b,c)$$$ of the form defined above with all possible integer constants $$$k$$$.


Pell's equations

Another somewhat recurring theme is Pell's equations. These are the equations that look like

$$$ x^2 - Dy^2 = 1. $$$

We're looking for positive integer solutions $$$(x, y)$$$ to it. To understand the solutions better, we can relax it to inequalities

$$$ 0 < x^2 - Dy^2 \leq 1. $$$

The two inequalities above are equivalent to the equation when $$$(x, y)$$$ are restricted to integers. Now, by dividing with $$$y^2$$$ we turn it into

$$$ \sqrt D < \frac{x}{y} \leq \sqrt{D+\frac{1}{y^2}}. $$$

The only possible lattice points satisfying the inequalities above are actual solutions to $$$x^2 - Dy^2 = 1$$$. It means that the set of lattice points in the quater-plane $$$x, y \geq 0$$$, for which $$$x > y \sqrt D$$$, is a subset of the area surrounded by the curve $$$x^2 - Dy^2 = 1$$$. As this area is strictly convex (see the picture below), the subset can only intersect the boundary of the area in the convex hull of the subset. In other words, all solutions must lie on the convex hull of lattice points $$$x > y \sqrt D$$$ on $$$x, y \geq 0$$$.


Convergents (red) correspond to corners of the convex hull of lattice points

From the theory of continued fractions it is known that such points are exactly the convergents $$$\frac{x}{y}$$$ of $$$\sqrt D$$$.

Solutions as a subgroup of $$$2 \times 2$$$ matrices with determinant $$$1$$$

The text above shows that if solution exists, it must be a convergent of $$$\sqrt D$$$. But still, we would like to know which particular convergents are the solutions and what is their general structure. To better understand it, let's rewrite the equation as

$$$ \det \begin{pmatrix} x & Dy \\ y & x \end{pmatrix} = 1. $$$

That's a very nice representation because of the following facts:

  1. If an integer matrix has determinant $$$1$$$, its inverse is also an integer matrix with determinant $$$1$$$.
  2. If two matrices have determinant $$$1$$$, so does their product.

So, let's consider the inverse of the matrix under the determinant. It is

$$$ \begin{pmatrix} x & Dy \\ y & x \end{pmatrix}^{-1} = \begin{pmatrix} x & -Dy \\ -y & x \end{pmatrix}, $$$

corresponding to the fact that if $$$(x, y)$$$ is an integer solution to the Pell's equation, so is $$$(x, -y)$$$. Now to the product:

$$$ \begin{pmatrix} x_0 & Dy_0 \\ y_0 & x_0 \end{pmatrix} \begin{pmatrix} x_1 & Dy_1 \\ y_1 & x_1 \end{pmatrix} = \begin{pmatrix} x_0 x_1 + D y_0 y_1 & D(x_0 y_1 + y_0 x_1) \\ x_0 y_1 + y_0 x_1 & x_0 x_1 + D y_0 y_1 \end{pmatrix} $$$

In other words, if $$$(x_0, y_0)$$$ and $$$(x_1,y_1)$$$ are solutions, so is $$$(x_0 x_1 + D y_0 y_1, x_0 y_1 + y_0 x_1)$$$. This means that the full set of integer solutions to the Pell's equations in fact form a group with the group operation defined as above. Note that the point $$$(x, y) = (1, 0)$$$ corresponds to the identity element of such group. Note that for $$$(x_0, y_0) = (x_1, y_1)$$$ the result is $$$(x^2+Dy^2, 2xy)$$$.

Solutions as a subgroup of $$$\mathbb Z[\sqrt D]$$$

Another, perhaps simpler way to look on it is to consider numbers of kind $$$a + b \sqrt D$$$. For such numbers, it's natural to define multiplication

$$$ (a+b \sqrt D) (c + d \sqrt D) = (ac + bd D) + (ac + bd) \sqrt D $$$

and the multiplicative norm $$$|a + b \sqrt D | = (a + b \sqrt D)(a - b \sqrt D) = a^2 - b^2 D$$$. Then it's also quite easy to see that the numbers satisfying $$$|a + b \sqrt D | = 1$$$ form a group, as the norm is multiplicative, that is $$$| AB | = |A | \cdot |B|$$$.

Periodicity of the continued fraction

Consider the continued fraction $$$\sqrt D = [a_0; a_1, a_2, \dots]$$$ and its complete quotients $$$s_k = [a_k; a_{k+1}, \dots]$$$. We can represent them as

$$$ s_k = \frac{\sqrt D + x_k}{y_k}, $$$

where $$$x_k$$$ and $$$y_k$$$ are rational numbers. In fact, we can even prove that $$$x_k$$$ and $$$y_k$$$ are integers, and that

$$$ \boxed{ \begin{align} D q_{k}^2 - p_{k}^2 &= y_{k+1} (-1)^{k+1}, \\ D q_k q_{k-1} - p_k p_{k-1} &= x_{k+1} (-1)^{k} \end{align}} $$$

for any convergent $$$\frac{p_{k-1}}{q_{k-1}} = [a_0; a_1, \dots, a_{k-1}]$$$. The proof is a bit technically involved, thus I will place it under the spoiler.

Proof

The identity above means that $$$|D q_k^2 - p_k^2| = 1$$$ if and only if $$$y_{k+1} = 1$$$, that is $$$s_{k+1} = \sqrt D + x_{k+1}$$$. As it turns out, this is tightly related to the periodicity of the continued fraction of $$$\sqrt D$$$, and only ever happens at $$$k=-1$$$ or after we just completed another period. To better understand this, we should prove the following fact:

$$$ x + y \sqrt D = [(a_0; a_1, \dots, a_k)] \iff \begin{cases} x + y \sqrt D > 1, \\ -1 < x - y \sqrt D < 0 \end{cases} $$$

That is, $$$x + y \sqrt D \in \mathbb Q[\sqrt D]$$$ has a periodic fraction if and only if it's larger than $$$1$$$ and $$$-1 < x - y \sqrt D < 0$$$.

Proof

The result above allows us to prove that $$$\sqrt{D}$$$ is periodic starting with $$$a_1$$$, because $$$-1 < s_1^* < 0$$$ from the very beginning. It, in turn, means that when we get to $$$s_{k+1} = \sqrt{D} + x_{k+1}$$$ it must hold that $$$x_{k+1} = \lfloor \sqrt D \rfloor = a_0$$$, otherwise $$$s_{k+1}^*$$$ won't be in the interval $$$(-1, 0)$$$. But this immediately tells us that $$$s_{k+2} = s_1$$$.

Finding all solutions

In other words, the first non-trivial solution to $$$|x^2 - y^2 D|=1$$$ is given by the convergent $$$\frac{p_k}{q_k}$$$, where

$$$ \sqrt D = [a_0; (a_1, \dots, a_{k+1})], $$$

and all the non-negative solutions are the convergents with indices $$$t(k+1)-1$$$ for integer $$$t \geq 0$$$. This observation also allows to find the explicit correspondence between a convergent $$$\frac{p_k}{q_k}$$$ and its matrix representation. It generally holds for convergents that

$$$ \begin{pmatrix} p_{k+1} & p_{k} \\ q_{k+1} & q_{k} \end{pmatrix} = \begin{pmatrix} a_0 & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} a_1 & 1 \\ 1 & 0 \end{pmatrix} \dots \begin{pmatrix} a_{k+1} & 1 \\ 1 & 0 \end{pmatrix}. $$$

If $$$|p_k^2 - D q_k^2| = 1$$$, we can additionally derive that

$$$ \begin{pmatrix} p_{k} & Dq_{k} \\ q_{k} & p_{k} \end{pmatrix} = \begin{pmatrix} a_0 & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} a_1 & 1 \\ 1 & 0 \end{pmatrix} \dots \begin{pmatrix} a_{k+1} & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} a_0 & 1 \\ 1 & 0 \end{pmatrix}^{-1}. $$$
Proof

It makes a lot of sense, as applying the transform defined by such matrix to any convergent $$$\small \frac{p_t}{q_t}$$$ will simply add another period in front of it, thus moving it into $$$\small \frac{p_{t+k+1}}{q_{t+k+1}}$$$. This gives explicit isomorphism between addition in convergent's indices and group operation in the solution group in terms of matrices. This also highlights that the solution group is cyclic, that is all solutions are obtained as powers of the smallest solution $$$\frac{p_k}{q_k}$$$, which can be found with continued fractions.

Summary

In other words, to find all solutions to the Pell's equation $$$x^2 - Dy^2=1$$$, one may find the smallest solution as the first convergent $$$\small \frac{p_k}{q_k}$$$ of $$$\sqrt D = [a_0; a_1, a_2, \dots]$$$ such that $$$p_k^2 - D q_k^2 = 1$$$. Then all the other solutions are obtained as

$$$ x + y \sqrt D = (p_k + q_k \sqrt D)^t $$$

for all integer $$$t \geq 0$$$.

Теги project euler, mathematics, continued fraction, diophantine equation, tutorial

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en20 Английский adamant 2023-05-16 15:35:17 77
en19 Английский adamant 2023-05-08 19:25:54 201
en18 Английский adamant 2023-05-08 18:36:48 96
en17 Английский adamant 2023-05-08 18:33:09 821
en16 Английский adamant 2023-05-08 17:51:35 6
en15 Английский adamant 2023-05-08 17:50:26 2
en14 Английский adamant 2023-05-08 00:57:05 176
en13 Английский adamant 2023-05-08 00:55:14 128
en12 Английский adamant 2023-05-08 00:45:14 5
en11 Английский adamant 2023-05-08 00:43:39 34
en10 Английский adamant 2023-05-08 00:42:33 11
en9 Английский adamant 2023-05-08 00:41:59 342
en8 Английский adamant 2023-05-07 21:07:14 66
en7 Английский adamant 2023-05-07 21:00:13 87
en6 Английский adamant 2023-05-07 20:58:32 49
en5 Английский adamant 2023-05-07 20:57:37 18
en4 Английский adamant 2023-05-07 20:46:04 377
en3 Английский adamant 2023-05-07 20:30:39 14
en2 Английский adamant 2023-05-07 20:27:57 1727
en1 Английский adamant 2023-05-07 20:00:24 13067 Initial revision (published)