The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V12. Number theory

Mathlib.NumberTheory.Pell

60 declarations. Mathlib docs · Source

Pell's Equation

Pell's Equation is the equation $x^2 - d y^2 = 1$, where $d$ is a positive integer that is not a square, and one is interested in solutions in integers $x$ and $y$.

In this file, we aim at providing all of the essential theory of Pell's Equation for general $d$ (as opposed to the contents of NumberTheory.PellMatiyasevic, which is specific to the case $d = a^2 - 1$ for some $a > 1$).

We begin by defining a type Pell.Solution₁ d for solutions of the equation, show that it has a natural structure as an abelian group, and prove some basic properties.

We then prove the following

Theorem. Let $d$ be a positive integer that is not a square. Then the equation $x^2 - d y^2 = 1$ has a nontrivial (i.e., with $y \ne 0$) solution in integers.

See Pell.exists_of_not_isSquare and Pell.Solution₁.exists_nontrivial_of_not_isSquare.

We then define the fundamental solution to be the solution with smallest $x$ among all solutions satisfying $x > 1$ and $y > 0$. We show that every solution is a power (in the sense of the group structure mentioned above) of the fundamental solution up to a (common) sign, see Pell.IsFundamental.eq_zpow_or_neg_zpow, and that a (positive) solution has this property if and only if it is fundamental, see Pell.pos_generator_iff_fundamental.

References

  • [K. Ireland, M. Rosen, A classical introduction to modern number theory (Section 17.5)][IrelandRosen1990]

Tags

Pell's equation

TODO

  • Extend to x ^ 2 - d * y ^ 2 = -1 and further generalizations.
  • Connect solutions to the continued fraction expansion of √d.

Group structure of the solution set

We define a structure of a commutative multiplicative group with distributive negation on the set of all solutions to the Pell equation x^2 - d*y^2 = 1.

The type of such solutions is Pell.Solution₁ d. It corresponds to a pair of integers x and y and a proof that (x, y) is indeed a solution.

The multiplication is given by (x, y) * (x', y') = (x*x' + d*y*y', x*y' + y*x'). This is obtained by mapping (x, y) to x + y*√d and multiplying the results. In fact, we define Pell.Solution₁ d to be ↥(unitary (ℤ√d)) and transport the "commutative group with distributive negation" structure from ↥(unitary (ℤ√d)).

We then set up an API for Pell.Solution₁ d.

Existence of nontrivial solutions

Fundamental solutions

We define the notion of a fundamental solution of Pell's equation and show that it exists and is unique (when d is positive and non-square) and generates the group of solutions up to sign.

theorem Pell.Solution₁.mk.congr_simp E1 #

∀ {d : ℤ} (x x_1 : ℤ) (e_x : x = x_1) (y y_1 : ℤ) (e_y : y = y_1) (prop : x ^ 2 - d * y ^ 2 = 1),
  Pell.Solution₁.mk x y prop = Pell.Solution₁.mk x_1 y_1 ⋯

theorem Pell.is_pell_solution_iff_mem_unitary E1 #

∀ {d : ℤ} {a : ℤ√d}, a.re ^ 2 - d * a.im ^ 2 = 1 ↔ a ∈ unitary (ℤ√d)

An element of ℤ√d has norm one (i.e., a.re^2 - d*a.im^2 = 1) if and only if it is contained in the submonoid of unitary elements.

TODO: merge this result with Pell.isPell_iff_mem_unitary.

def Pell.Solution₁ E1 #

ℤ → Type

Pell.Solution₁ d is the type of solutions to the Pell equation x^2 - d*y^2 = 1. We define this in terms of elements of ℤ√d of norm one.

instance Pell.Solution₁.instCommGroup E0 #

{d : ℤ} → CommGroup (Pell.Solution₁ d)

instance Pell.Solution₁.instHasDistribNeg E0 #

{d : ℤ} → HasDistribNeg (Pell.Solution₁ d)

instance Pell.Solution₁.instInhabited E0 #

{d : ℤ} → Inhabited (Pell.Solution₁ d)

instance Pell.Solution₁.instCoeZsqrtd E0 #

{d : ℤ} → Coe (Pell.Solution₁ d) (ℤ√d)

def Pell.Solution₁.x E1 #

{d : ℤ} → Pell.Solution₁ d → ℤ

The x component of a solution to the Pell equation x^2 - d*y^2 = 1

def Pell.Solution₁.y E1 #

{d : ℤ} → Pell.Solution₁ d → ℤ

The y component of a solution to the Pell equation x^2 - d*y^2 = 1

theorem Pell.Solution₁.prop E1 #

∀ {d : ℤ} (a : Pell.Solution₁ d), a.x ^ 2 - d * a.y ^ 2 = 1

The proof that a is a solution to the Pell equation x^2 - d*y^2 = 1

theorem Pell.Solution₁.prop_x E1 #

∀ {d : ℤ} (a : Pell.Solution₁ d), a.x ^ 2 = 1 + d * a.y ^ 2

An alternative form of the equation, suitable for rewriting x^2.

theorem Pell.Solution₁.prop_y E1 #

∀ {d : ℤ} (a : Pell.Solution₁ d), d * a.y ^ 2 = a.x ^ 2 - 1

An alternative form of the equation, suitable for rewriting d * y^2.

theorem Pell.Solution₁.ext E1 #

∀ {d : ℤ} {a b : Pell.Solution₁ d}, a.x = b.x → a.y = b.y → a = b

Two solutions are equal if their x and y components are equal.

theorem Pell.Solution₁.ext_iff E0 #

∀ {d : ℤ} {a b : Pell.Solution₁ d}, a = b ↔ a.x = b.x ∧ a.y = b.y

def Pell.Solution₁.mk E1 #

{d : ℤ} → (x y : ℤ) → x ^ 2 - d * y ^ 2 = 1 → Pell.Solution₁ d

Construct a solution from x, y and a proof that the equation is satisfied.

theorem Pell.Solution₁.x_mk E0 #

∀ {d : ℤ} (x y : ℤ) (prop : x ^ 2 - d * y ^ 2 = 1), (Pell.Solution₁.mk x y prop).x = x

theorem Pell.Solution₁.y_mk E0 #

∀ {d : ℤ} (x y : ℤ) (prop : x ^ 2 - d * y ^ 2 = 1), (Pell.Solution₁.mk x y prop).y = y

theorem Pell.Solution₁.coe_mk E0 #

∀ {d : ℤ} (x y : ℤ) (prop : x ^ 2 - d * y ^ 2 = 1), ↑(Pell.Solution₁.mk x y prop) = { re := x, im := y }

theorem Pell.Solution₁.x_one E0 #

∀ {d : ℤ}, Pell.Solution₁.x 1 = 1

theorem Pell.Solution₁.y_one E0 #

∀ {d : ℤ}, Pell.Solution₁.y 1 = 0

theorem Pell.Solution₁.x_mul E0 #

∀ {d : ℤ} (a b : Pell.Solution₁ d), (a * b).x = a.x * b.x + d * (a.y * b.y)

theorem Pell.Solution₁.y_mul E0 #

∀ {d : ℤ} (a b : Pell.Solution₁ d), (a * b).y = a.x * b.y + a.y * b.x

theorem Pell.Solution₁.x_inv E0 #

∀ {d : ℤ} (a : Pell.Solution₁ d), a⁻¹.x = a.x

theorem Pell.Solution₁.y_inv E0 #

∀ {d : ℤ} (a : Pell.Solution₁ d), a⁻¹.y = -a.y

theorem Pell.Solution₁.x_neg E0 #

∀ {d : ℤ} (a : Pell.Solution₁ d), (-a).x = -a.x

theorem Pell.Solution₁.y_neg E0 #

∀ {d : ℤ} (a : Pell.Solution₁ d), (-a).y = -a.y

theorem Pell.Solution₁.x_ne_zero E1 #

∀ {d : ℤ}, 0 ≤ d → ∀ (a : Pell.Solution₁ d), a.x ≠ 0

A solution has x ≠ 0.

theorem Pell.Solution₁.y_ne_zero_of_one_lt_x E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 1 < a.x → a.y ≠ 0

A solution with x > 1 must have y ≠ 0.

theorem Pell.Solution₁.d_pos_of_one_lt_x E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 1 < a.x → 0 < d

If a solution has x > 1, then d is positive.

theorem Pell.Solution₁.d_nonsquare_of_one_lt_x E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 1 < a.x → ¬IsSquare d

If a solution has x > 1, then d is not a square.

theorem Pell.Solution₁.x_mul_pos E1 #

∀ {d : ℤ} {a b : Pell.Solution₁ d}, 0 < a.x → 0 < b.x → 0 < (a * b).x

The set of solutions with x > 0 is closed under multiplication.

theorem Pell.Solution₁.y_mul_pos E1 #

∀ {d : ℤ} {a b : Pell.Solution₁ d}, 0 < a.x → 0 < a.y → 0 < b.x → 0 < b.y → 0 < (a * b).y

The set of solutions with x and y positive is closed under multiplication.

theorem Pell.Solution₁.x_pow_pos E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 0 < a.x → ∀ (n : ℕ), 0 < (a ^ n).x

If (x, y) is a solution with x positive, then all its powers with natural exponents have positive x.

theorem Pell.Solution₁.y_pow_succ_pos E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 0 < a.x → 0 < a.y → ∀ (n : ℕ), 0 < (a ^ n.succ).y

If (x, y) is a solution with x and y positive, then all its powers with positive natural exponents have positive y.

theorem Pell.Solution₁.y_zpow_pos E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 0 < a.x → 0 < a.y → ∀ {n : ℤ}, 0 < n → 0 < (a ^ n).y

If (x, y) is a solution with x and y positive, then all its powers with positive exponents have positive y.

theorem Pell.Solution₁.x_zpow_pos E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 0 < a.x → ∀ (n : ℤ), 0 < (a ^ n).x

If (x, y) is a solution with x positive, then all its powers have positive x.

theorem Pell.Solution₁.sign_y_zpow_eq_sign_of_x_pos_of_y_pos E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, 0 < a.x → 0 < a.y → ∀ (n : ℤ), (a ^ n).y.sign = n.sign

If (x, y) is a solution with x and y positive, then the y component of any power has the same sign as the exponent.

theorem Pell.Solution₁.exists_pos_variant E1 #

∀ {d : ℤ}, 0 < d → ∀ (a : Pell.Solution₁ d), ∃ b, 0 < b.x ∧ 0 ≤ b.y ∧ a ∈ {b, b⁻¹, -b, -b⁻¹}

If a is any solution, then one of a, a⁻¹, -a, -a⁻¹ has positive x and nonnegative y.

theorem Pell.exists_of_not_isSquare E3 #

∀ {d : ℤ}, 0 < d → ¬IsSquare d → ∃ x y, x ^ 2 - d * y ^ 2 = 1 ∧ y ≠ 0

If d is a positive integer that is not a square, then there is a nontrivial solution to the Pell equation x^2 - d*y^2 = 1.

theorem Pell.exists_iff_not_isSquare E1 #

∀ {d : ℤ}, 0 < d → ((∃ x y, x ^ 2 - d * y ^ 2 = 1 ∧ y ≠ 0) ↔ ¬IsSquare d)

If d is a positive integer, then there is a nontrivial solution to the Pell equation x^2 - d*y^2 = 1 if and only if d is not a square.

theorem Pell.Solution₁.exists_nontrivial_of_not_isSquare E1 #

∀ {d : ℤ}, 0 < d → ¬IsSquare d → ∃ a, a ≠ 1 ∧ a ≠ -1

If d is a positive integer that is not a square, then there exists a nontrivial solution to the Pell equation x^2 - d*y^2 = 1.

theorem Pell.Solution₁.exists_pos_of_not_isSquare E1 #

∀ {d : ℤ}, 0 < d → ¬IsSquare d → ∃ a, 1 < a.x ∧ 0 < a.y

If d is a positive integer that is not a square, then there exists a solution to the Pell equation x^2 - d*y^2 = 1 with x > 1 and y > 0.

def Pell.IsFundamental E1 #

{d : ℤ} → Pell.Solution₁ d → Prop

We define a solution to be fundamental if it has x > 1 and y > 0 and its x is the smallest possible among solutions with x > 1.

theorem Pell.IsFundamental.x_pos E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, Pell.IsFundamental a → 0 < a.x

A fundamental solution has positive x.

theorem Pell.IsFundamental.d_pos E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, Pell.IsFundamental a → 0 < d

If a fundamental solution exists, then d must be positive.

theorem Pell.IsFundamental.d_nonsquare E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, Pell.IsFundamental a → ¬IsSquare d

If a fundamental solution exists, then d must be a non-square.

theorem Pell.IsFundamental.subsingleton E1 #

∀ {d : ℤ} {a b : Pell.Solution₁ d}, Pell.IsFundamental a → Pell.IsFundamental b → a = b

If there is a fundamental solution, it is unique.

theorem Pell.IsFundamental.exists_of_not_isSquare E1 #

∀ {d : ℤ}, 0 < d → ¬IsSquare d → ∃ a, Pell.IsFundamental a

If d is positive and not a square, then a fundamental solution exists.

theorem Pell.IsFundamental.y_strictMono E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, Pell.IsFundamental a → StrictMono fun n ↦ (a ^ n).y

The map sending an integer n to the y-coordinate of a^n for a fundamental solution a is strictly increasing.

theorem Pell.IsFundamental.zpow_y_lt_iff_lt E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, Pell.IsFundamental a → ∀ (m n : ℤ), (a ^ m).y < (a ^ n).y ↔ m < n

If a is a fundamental solution, then (a^m).y < (a^n).y if and only if m < n.

theorem Pell.IsFundamental.zpow_eq_one_iff E0 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, Pell.IsFundamental a → ∀ (n : ℤ), a ^ n = 1 ↔ n = 0

The nth power of a fundamental solution is trivial if and only if n = 0.

theorem Pell.IsFundamental.zpow_ne_neg_zpow E1 #

∀ {d : ℤ} {a : Pell.Solution₁ d}, Pell.IsFundamental a → ∀ {n n' : ℤ}, a ^ n ≠ -a ^ n'

A power of a fundamental solution is never equal to the negative of a power of this fundamental solution.

theorem Pell.IsFundamental.x_le_x E1 #

∀ {d : ℤ} {a₁ : Pell.Solution₁ d}, Pell.IsFundamental a₁ → ∀ {a : Pell.Solution₁ d}, 1 < a.x → a₁.x ≤ a.x

The x-coordinate of a fundamental solution is a lower bound for the x-coordinate of any positive solution.

theorem Pell.IsFundamental.y_le_y E1 #

∀ {d : ℤ} {a₁ : Pell.Solution₁ d}, Pell.IsFundamental a₁ → ∀ {a : Pell.Solution₁ d}, 1 < a.x → 0 < a.y → a₁.y ≤ a.y

The y-coordinate of a fundamental solution is a lower bound for the y-coordinate of any positive solution.

theorem Pell.IsFundamental.x_mul_y_le_y_mul_x E1 #

∀ {d : ℤ} {a₁ : Pell.Solution₁ d},
  Pell.IsFundamental a₁ → ∀ {a : Pell.Solution₁ d}, 1 < a.x → 0 < a.y → a.x * a₁.y ≤ a.y * a₁.x

theorem Pell.IsFundamental.mul_inv_y_nonneg E1 #

∀ {d : ℤ} {a₁ : Pell.Solution₁ d},
  Pell.IsFundamental a₁ → ∀ {a : Pell.Solution₁ d}, 1 < a.x → 0 < a.y → 0 ≤ (a * a₁⁻¹).y

If we multiply a positive solution with the inverse of a fundamental solution, the y-coordinate remains nonnegative.

theorem Pell.IsFundamental.mul_inv_x_pos E1 #

∀ {d : ℤ} {a₁ : Pell.Solution₁ d},
  Pell.IsFundamental a₁ → ∀ {a : Pell.Solution₁ d}, 1 < a.x → 0 < a.y → 0 < (a * a₁⁻¹).x

If we multiply a positive solution with the inverse of a fundamental solution, the x-coordinate stays positive.

theorem Pell.IsFundamental.mul_inv_x_lt_x E1 #

∀ {d : ℤ} {a₁ : Pell.Solution₁ d},
  Pell.IsFundamental a₁ → ∀ {a : Pell.Solution₁ d}, 1 < a.x → 0 < a.y → (a * a₁⁻¹).x < a.x

If we multiply a positive solution with the inverse of a fundamental solution, the x-coordinate decreases.

theorem Pell.existsUnique_pos_generator E1 #

∀ {d : ℤ}, 0 < d → ¬IsSquare d → ∃! a₁, 1 < a₁.x ∧ 0 < a₁.y ∧ ∀ (a : Pell.Solution₁ d), ∃ n, a = a₁ ^ n ∨ a = -a₁ ^ n

When d is positive and not a square, then the group of solutions to the Pell equation x^2 - d*y^2 = 1 has a unique positive generator (up to sign).

theorem Pell.pos_generator_iff_fundamental E1 #

∀ {d : ℤ} (a : Pell.Solution₁ d),
  (1 < a.x ∧ 0 < a.y ∧ ∀ (b : Pell.Solution₁ d), ∃ n, b = a ^ n ∨ b = -a ^ n) ↔ Pell.IsFundamental a

A positive solution is a generator (up to sign) of the group of all solutions to the Pell equation x^2 - d*y^2 = 1 if and only if it is a fundamental solution.