Pell's Equation

lean4-proofnumber-theoryvisualization
x2dy2=1x^2 - d y^2 = 1

Statement

For a positive integer dd that is not a perfect square, Pell's equation

x2dy2=1x^2 - dy^2 = 1

has infinitely many integer solutions (x,y)(x, y) with x,y>0x, y > 0. The fundamental solution (x1,y1)(x_1, y_1) — the smallest positive one — generates all others via the recurrence

xn+1=x1xn+dy1yn,yn+1=x1yn+y1xn.x_{n+1} = x_1 x_n + d\, y_1 y_n, \qquad y_{n+1} = x_1 y_n + y_1 x_n.

The trivial solution (1,0)(1, 0) always exists. The interesting content is that a non-trivial solution exists when dd is not a perfect square.

Visualization

Solutions to x22y2=1x^2 - 2y^2 = 1, generated from the fundamental solution (3,2)(3, 2):

nnxnx_nyny_nxn22yn2x_n^2 - 2y_n^2
0101
13298=19 - 8 = 1
21712289288=1289 - 288 = 1
3997098019800=19801 - 9800 = 1
4577408332929332928=1332929 - 332928 = 1
5336323781130976911309768=111309769 - 11309768 = 1

Each row is produced by (xn+1,yn+1)=(3xn+4yn,  2xn+3yn)(x_{n+1}, y_{n+1}) = (3x_n + 4y_n,\; 2x_n + 3y_n).

Proof Sketch

  1. Pigeonhole / continued fractions. Consider the continued fraction expansion of d\sqrt{d}. Since dd is not a square, d\sqrt{d} is irrational and its continued fraction is periodic (by Lagrange's theorem).
  2. Convergents satisfy the equation. The convergents pk/qkp_k/q_k of d\sqrt{d} satisfy pkqkd<1/qk|p_k - q_k\sqrt{d}| < 1/q_k. A pigeonhole argument on the fractional parts of jdj\sqrt{d} for j=0,,d+1j = 0, \ldots, \lfloor\sqrt{d}\rfloor + 1 yields integers x,yx, y with x2dy22d|x^2 - dy^2| \le 2\sqrt{d}. Among these, by another pigeonhole step, one value of x2dy2x^2 - dy^2 repeats; subtracting gives a solution to Pell's equation.
  3. Infinitely many solutions. If (x1,y1)(x_1, y_1) is the fundamental solution, the map (x,y)(x1x+dy1y,  x1y+y1x)(x, y) \mapsto (x_1 x + d y_1 y,\; x_1 y + y_1 x) sends solutions to solutions, producing infinitely many.
  4. Group structure. The set of positive solutions forms a free abelian group of rank 1, generated by the fundamental solution.

Connections

Pell's equation is closely connected to Pythagorean TriplesPythagorean Triplesa=m2n2,  b=2mn,  c=m2+n2a = m^2 - n^2,\; b = 2mn,\; c = m^2 + n^2Complete parametrisation of all integer solutions to a² + b² = c²Read more →, which arise from a related norm-1 condition over Z[i]\mathbb{Z}[i]. The theory of solutions is an instance of the general theory underlying Quadratic ReciprocityQuadratic Reciprocity(pq)(qp)=(1)p12q12\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}Relationship between solvability of quadratic congruences for two odd primesRead more →, where quadratic residues determine which primes split in Z[d]\mathbb{Z}[\sqrt{d}].

Lean4 Proof

import Mathlib.NumberTheory.Pell

/-- The trivial solution (1, 0) always solves x² - dy² = 1. -/
theorem pell_trivial (d : ) : (1 : )^2 - d * (0 : )^2 = 1 := by ring

/-- The fundamental solution (3, 2) solves x² - 2y² = 1. -/
theorem pell_3_2 : (3 : )^2 - 2 * (2 : )^2 = 1 := by norm_num

/-- For positive non-square d, a nontrivial solution exists.
    Mathlib's `Pell.Solution₁.exists_pos_of_not_isSquare` gives a solution with x > 1, y > 0. -/
theorem pell_exists (d : ) (hd_pos : 0 < d) (hd_sq : ¬IsSquare d) :
     a : Pell.Solution₁ d, 1 < a.x  0 < a.y :=
  Pell.Solution₁.exists_pos_of_not_isSquare hd_pos hd_sq