Documentation

Mathlib.Data.Int.GCD

Extended GCD and divisibility over ℤ #

Main definitions #

Main statements #

Tags #

Bézout's lemma, Bezout's lemma

Extended Euclidean algorithm #

def Nat.xgcdAux :
× ×

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

Equations
    Instances For
      @[simp]
      theorem Nat.xgcd_zero_left {s t : } {r' : } {s' t' : } :
      xgcdAux 0 s t r' s' t' = (r', s', t')
      theorem Nat.xgcdAux_rec {r : } {s t : } {r' : } {s' t' : } (h : 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 (x y : ) :

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

      Equations
        Instances For
          def Nat.gcdA (x y : ) :

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

          Equations
            Instances For
              def Nat.gcdB (x y : ) :

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

              Equations
                Instances For
                  @[simp]
                  theorem Nat.gcdA_zero_left {s : } :
                  gcdA 0 s = 0
                  @[simp]
                  theorem Nat.gcdB_zero_left {s : } :
                  gcdB 0 s = 1
                  @[simp]
                  theorem Nat.gcdA_zero_right {s : } (h : s 0) :
                  s.gcdA 0 = 1
                  @[simp]
                  theorem Nat.gcdB_zero_right {s : } (h : s 0) :
                  s.gcdB 0 = 0
                  @[simp]
                  theorem Nat.xgcdAux_fst (x y : ) (s t s' t' : ) :
                  (x.xgcdAux s t y s' t').1 = x.gcd y
                  theorem Nat.xgcdAux_val (x y : ) :
                  x.xgcdAux 1 0 y 0 1 = (x.gcd y, x.xgcd y)
                  theorem Nat.xgcd_val (x y : ) :
                  x.xgcd y = (x.gcdA y, x.gcdB y)
                  theorem Nat.gcd_eq_gcd_ab (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 {k n : } (hk : n.gcd k < k) :
                  m < k, n * m % k = n.gcd k
                  @[deprecated Nat.exists_mul_mod_eq_gcd (since := "2025-11-01")]
                  theorem Nat.exists_mul_emod_eq_gcd {k n : } (hk : n.gcd k < k) :
                  m < k, n * m % k = n.gcd k

                  Alias of Nat.exists_mul_mod_eq_gcd.

                  theorem Nat.exists_mul_mod_eq_one_of_coprime {k n : } (hkn : n.Coprime k) (hk : 1 < k) :
                  m < k, n * m % k = 1
                  @[deprecated Nat.exists_mul_mod_eq_one_of_coprime (since := "2025-11-01")]
                  theorem Nat.exists_mul_emod_eq_one_of_coprime {k n : } (hkn : n.Coprime k) (hk : 1 < k) :
                  m < k, n * m % k = 1

                  Alias of Nat.exists_mul_mod_eq_one_of_coprime.

                  theorem Nat.exists_mul_mod_eq_of_coprime {k n : } (r : ) (hkn : n.Coprime k) (hk : k 0) :
                  m < k, n * m % k = r % k

                  Divisibility over ℤ #

                  theorem Int.gcd_def (i j : ) :
                  def Int.gcdA :

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

                  Equations
                    Instances For
                      def Int.gcdB :

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

                      Equations
                        Instances For
                          theorem Int.gcd_eq_gcd_ab (x y : ) :
                          (x.gcd y) = x * x.gcdA y + y * x.gcdB y

                          Bézout's lemma

                          theorem Int.lcm_def (i j : ) :
                          theorem Int.gcd_div {a b c : } (ha : c a) (hb : c b) :
                          (a / c).gcd (b / c) = a.gcd b / c.natAbs

                          Alias of Int.gcd_ediv.

                          theorem Int.gcd_div_gcd_div_gcd {i j : } (h : 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 {a : } {m n : } (h : 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 {a : } {m n : } (h : 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 {x y : } (hc : x.gcd y 0) :
                          x 0 y 0
                          theorem Int.exists_gcd_one {m n : } (H : 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' {m n : } (H : 0 < m.gcd n) :
                          ∃ (g : ) (m' : ) (n' : ), 0 < g m'.gcd n' = 1 m = m' * g n = n' * g
                          theorem Int.gcd_dvd_iff {a b : } {n : } :
                          a.gcd b n ∃ (x : ) (y : ), n = a * x + b * y
                          theorem Int.gcd_greatest {a b d : } (hd_pos : 0 d) (hda : d a) (hdb : d b) (hd : ∀ (e : ), e ae be d) :
                          d = (a.gcd b)
                          theorem Int.dvd_of_dvd_mul_left_of_gcd_one {a b c : } (habc : a b * c) (hab : 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 {a b c : } (habc : a b * c) (hab : 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 {a b : } (ha : 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

                          @[simp]
                          theorem pow_gcd_eq_one {M : Type u_1} [Monoid M] {a : M} {m n : } :
                          a ^ m.gcd n = 1 a ^ m = 1 a ^ n = 1
                          @[simp]
                          theorem gcd_nsmul_eq_zero {M : Type u_1} [AddMonoid M] {a : M} {m n : } :
                          m.gcd n a = 0 m a = 0 n a = 0
                          theorem pow_eq_one_iff_of_coprime {M : Type u_1} [Monoid M] {a : M} {m n : } (hmn : m.Coprime n) :
                          a ^ m = 1 a ^ n = 1 a = 1
                          theorem nsmul_eq_zero_iff_of_coprime {M : Type u_1} [AddMonoid M] {a : M} {m n : } (hmn : m.Coprime n) :
                          m a = 0 n a = 0 a = 0
                          @[simp]
                          theorem pow_intGCD_eq_one {M : Type u_1} [Group M] {a : M} {m n : } :
                          a ^ m.gcd n = 1 a ^ m = 1 a ^ n = 1
                          @[simp]
                          theorem intGCD_nsmul_eq_zero {M : Type u_1} [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 {α : Type u_1} [GroupWithZero α] {a b : α} {m n : } (hab : Commute a b) (hmn : m.Coprime n) :
                          a ^ m = b ^ n ∃ (c : α), a = c ^ n b = c ^ m
                          theorem pow_eq_pow_iff_of_coprime {α : Type u_1} [CommGroupWithZero α] {a b : α} {m n : } (hmn : m.Coprime n) :
                          a ^ m = b ^ n ∃ (c : α), a = c ^ n b = c ^ m
                          theorem pow_mem_range_pow_of_coprime {α : Type u_1} [CommGroupWithZero α] {m n : } (hmn : m.Coprime n) (a : α) :
                          (a ^ m Set.range fun (x : α) => x ^ n) a Set.range fun (x : α) => x ^ n