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 / V02. Numbers

Mathlib.Data.Int.GCD

45 declarations. Mathlib docs · Source

Extended GCD and divisibility over ℤ

Main definitions

  • Given x y : ℕ, xgcd x y computes the pair of integers (a, b) such that gcd x y = x * a + y * b. gcdA x y and gcdB x y are defined to be a and b, respectively.

Main statements

  • gcd_eq_gcd_ab: Bézout's lemma, given x y : ℕ, gcd x y = x * gcdA x y + y * gcdB x y.

Tags

Bézout's lemma, Bezout's lemma

Extended Euclidean algorithm

Divisibility over ℤ

theorem Nat.xgcdAux.match_1.congr_eq_1 E1 #

∀ (motive : (n : ℕ) → ((m : ℕ) → m < n → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ) → Sort u_1) (n : ℕ)
  (ih : (m : ℕ) → m < n → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ)
  (h_1 : (ih : (m : ℕ) → m < 0 → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ) → motive 0 ih)
  (h_2 : (k : ℕ) → (ih : (m : ℕ) → m < k.succ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ) → motive k.succ ih)
  (ih_1 : (m : ℕ) → m < 0 → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ),
  n = 0 →
    ih ≍ ih_1 →
      (match n, ih with
        | 0, ih => h_1 ih
        | k.succ, ih => h_2 k ih) ≍
        h_1 ih_1

theorem Nat.xgcdAux.match_1.congr_eq_2 E1 #

∀ (motive : (n : ℕ) → ((m : ℕ) → m < n → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ) → Sort u_1) (n : ℕ)
  (ih : (m : ℕ) → m < n → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ)
  (h_1 : (ih : (m : ℕ) → m < 0 → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ) → motive 0 ih)
  (h_2 : (k : ℕ) → (ih : (m : ℕ) → m < k.succ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ) → motive k.succ ih) (k : ℕ)
  (ih_1 : (m : ℕ) → m < k.succ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ),
  n = k.succ →
    ih ≍ ih_1 →
      (match n, ih with
        | 0, ih => h_1 ih
        | k.succ, ih => h_2 k ih) ≍
        h_2 k ih_1

def Nat.xgcdAux E0 #

ℕ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ

Helper function for the extended GCD algorithm (Nat.xgcd).

theorem Nat.xgcd_zero_left E0 #

∀ {s t : ℤ} {r' : ℕ} {s' t' : ℤ}, Nat.xgcdAux 0 s t r' s' t' = (r', s', t')

theorem Nat.xgcdAux_rec E1 #

∀ {r : ℕ} {s t : ℤ} {r' : ℕ} {s' t' : ℤ},
  0 < r → r.xgcdAux s t r' s' t' = (r' % r).xgcdAux (s' - ↑r' / ↑r * s) (t' - ↑r' / ↑r * t) r s t

def Nat.xgcd E3 #

ℕ → ℕ → ℤ × ℤ

Use the extended GCD algorithm to generate the a and b values satisfying gcd x y = x * a + y * b.

def Nat.gcdA E1 #

ℕ → ℕ → ℤ

The extended GCD a value in the equation gcd x y = x * a + y * b.

def Nat.gcdB E1 #

ℕ → ℕ → ℤ

The extended GCD b value in the equation gcd x y = x * a + y * b.

theorem Nat.gcdA_zero_left E0 #

∀ {s : ℕ}, Nat.gcdA 0 s = 0

theorem Nat.gcdB_zero_left E0 #

∀ {s : ℕ}, Nat.gcdB 0 s = 1

theorem Nat.gcdA_zero_right E0 #

∀ {s : ℕ}, s ≠ 0 → s.gcdA 0 = 1

theorem Nat.gcdB_zero_right E0 #

∀ {s : ℕ}, s ≠ 0 → s.gcdB 0 = 0

theorem Nat.xgcdAux_fst E0 #

∀ (x y : ℕ) (s t s' t' : ℤ), (x.xgcdAux s t y s' t').1 = x.gcd y

theorem Nat.xgcdAux_val E0 #

∀ (x y : ℕ), x.xgcdAux 1 0 y 0 1 = (x.gcd y, x.xgcd y)

theorem Nat.xgcd_val E0 #

∀ (x y : ℕ), x.xgcd y = (x.gcdA y, x.gcdB y)

theorem Nat.gcd_eq_gcd_ab E3 #

∀ (x y : ℕ), ↑(x.gcd y) = ↑x * x.gcdA y + ↑y * x.gcdB y

Bézout's lemma: given x y : ℕ, gcd x y = x * a + y * b, where a = gcd_a x y and b = gcd_b x y are computed by the extended Euclidean algorithm.

theorem Nat.exists_mul_mod_eq_gcd E1 #

∀ {k n : ℕ}, n.gcd k < k → ∃ m < k, n * m % k = n.gcd k

theorem Nat.exists_mul_mod_eq_one_of_coprime E1 #

∀ {k n : ℕ}, n.Coprime k → 1 < k → ∃ m < k, n * m % k = 1

theorem Nat.exists_mul_mod_eq_of_coprime E1 #

∀ {k n : ℕ} (r : ℕ), n.Coprime k → k ≠ 0 → ∃ m < k, n * m % k = r % k

theorem Int.gcd_def E0 #

∀ (i j : ℤ), i.gcd j = i.natAbs.gcd j.natAbs

def Int.gcdA E1 #

ℤ → ℤ → ℤ

The extended GCD a value in the equation gcd x y = x * a + y * b.

def Int.gcdB E1 #

ℤ → ℤ → ℤ

The extended GCD b value in the equation gcd x y = x * a + y * b.

theorem Int.gcd_eq_gcd_ab E3 #

∀ (x y : ℤ), ↑(x.gcd y) = x * x.gcdA y + y * x.gcdB y

Bézout's lemma

theorem Int.lcm_def E0 #

∀ (i j : ℤ), i.lcm j = i.natAbs.lcm j.natAbs

theorem Int.gcd_div E1 #

∀ {a b c : ℤ}, c ∣ a → c ∣ b → (a / c).gcd (b / c) = a.gcd b / c.natAbs

Alias of Int.gcd_ediv.

theorem Int.gcd_div_gcd_div_gcd E1 #

∀ {i j : ℤ}, 0 < i.gcd j → (i / ↑(i.gcd j)).gcd (j / ↑(i.gcd j)) = 1

Alias of Int.gcd_ediv_gcd_ediv_gcd.

theorem Int.gcd_eq_one_of_gcd_mul_right_eq_one_left E1 #

∀ {a : ℤ} {m n : ℕ}, a.gcd (↑m * ↑n) = 1 → a.gcd ↑m = 1

If gcd a (m * n) = 1, then gcd a m = 1.

theorem Int.gcd_eq_one_of_gcd_mul_right_eq_one_right E1 #

∀ {a : ℤ} {m n : ℕ}, a.gcd (↑m * ↑n) = 1 → a.gcd ↑n = 1

If gcd a (m * n) = 1, then gcd a n = 1.

theorem Int.ne_zero_of_gcd E1 #

∀ {x y : ℤ}, x.gcd y ≠ 0 → x ≠ 0 ∨ y ≠ 0

theorem Int.exists_gcd_one E1 #

∀ {m n : ℤ}, 0 < m.gcd n → ∃ m' n', m'.gcd n' = 1 ∧ m = m' * ↑(m.gcd n) ∧ n = n' * ↑(m.gcd n)

theorem Int.exists_gcd_one' E1 #

∀ {m n : ℤ}, 0 < m.gcd n → ∃ g m' n', 0 < g ∧ m'.gcd n' = 1 ∧ m = m' * ↑g ∧ n = n' * ↑g

theorem Int.gcd_dvd_iff E0 #

∀ {a b : ℤ} {n : ℕ}, a.gcd b ∣ n ↔ ∃ x y, ↑n = a * x + b * y

theorem Int.gcd_greatest E1 #

∀ {a b d : ℤ}, 0 ≤ d → d ∣ a → d ∣ b → (∀ (e : ℤ), e ∣ a → e ∣ b → e ∣ d) → d = ↑(a.gcd b)

theorem Int.dvd_of_dvd_mul_left_of_gcd_one E2 #

∀ {a b c : ℤ}, a ∣ b * c → a.gcd c = 1 → a ∣ b

Euclid's lemma: if a ∣ b * c and gcd a c = 1 then a ∣ b. Compare with IsCoprime.dvd_of_dvd_mul_left and UniqueFactorizationMonoid.dvd_of_dvd_mul_left_of_no_prime_factors

theorem Int.dvd_of_dvd_mul_right_of_gcd_one E2 #

∀ {a b c : ℤ}, a ∣ b * c → a.gcd b = 1 → a ∣ c

Euclid's lemma: if a ∣ b * c and gcd a b = 1 then a ∣ c. Compare with IsCoprime.dvd_of_dvd_mul_right and UniqueFactorizationMonoid.dvd_of_dvd_mul_right_of_no_prime_factors

theorem Int.gcd_least_linear E1 #

∀ {a b : ℤ}, a ≠ 0 → IsLeast {n | 0 < n ∧ ∃ x y, ↑n = a * x + b * y} (a.gcd b)

For nonzero integers a and b, gcd a b is the smallest positive natural number that can be written in the form a * x + b * y for some pair of integers x and y

theorem pow_gcd_eq_one E0 #

∀ {M : Type u_1} [inst : Monoid M] {a : M} {m n : ℕ}, a ^ m.gcd n = 1 ↔ a ^ m = 1 ∧ a ^ n = 1

theorem gcd_nsmul_eq_zero E0 #

∀ {M : Type u_1} [inst : AddMonoid M] {a : M} {m n : ℕ}, m.gcd n • a = 0 ↔ m • a = 0 ∧ n • a = 0

theorem pow_eq_one_iff_of_coprime E1 #

∀ {M : Type u_1} [inst : Monoid M] {a : M} {m n : ℕ}, m.Coprime n → (a ^ m = 1 ∧ a ^ n = 1 ↔ a = 1)

theorem nsmul_eq_zero_iff_of_coprime E1 #

∀ {M : Type u_1} [inst : AddMonoid M] {a : M} {m n : ℕ}, m.Coprime n → (m • a = 0 ∧ n • a = 0 ↔ a = 0)

theorem pow_intGCD_eq_one E0 #

∀ {M : Type u_1} [inst : Group M] {a : M} {m n : ℤ}, a ^ m.gcd n = 1 ↔ a ^ m = 1 ∧ a ^ n = 1

theorem intGCD_nsmul_eq_zero E0 #

∀ {M : Type u_1} [inst : AddGroup M] {a : M} {m n : ℤ}, m.gcd n • a = 0 ↔ m • a = 0 ∧ n • a = 0

theorem Commute.pow_eq_pow_iff_of_coprime E1 #

∀ {α : Type u_1} [inst : GroupWithZero α] {a b : α} {m n : ℕ},
  Commute a b → m.Coprime n → (a ^ m = b ^ n ↔ ∃ c, a = c ^ n ∧ b = c ^ m)

theorem pow_eq_pow_iff_of_coprime E1 #

∀ {α : Type u_1} [inst : CommGroupWithZero α] {a b : α} {m n : ℕ},
  m.Coprime n → (a ^ m = b ^ n ↔ ∃ c, a = c ^ n ∧ b = c ^ m)

theorem pow_mem_range_pow_of_coprime E1 #

∀ {α : Type u_1} [inst : CommGroupWithZero α] {m n : ℕ},
  m.Coprime n → ∀ (a : α), (a ^ m ∈ Set.range fun x ↦ x ^ n) ↔ a ∈ Set.range fun x ↦ x ^ n