|
| 1 | +/- |
| 2 | +Copyright (c) 2020 Kenny Lau. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Kenny Lau, Ken Lee, Chris Hughes |
| 5 | +-/ |
| 6 | + |
| 7 | +import algebra.big_operators |
| 8 | +import data.fintype.basic |
| 9 | +import data.int.gcd |
| 10 | +import data.set.disjointed |
| 11 | + |
| 12 | +/-! |
| 13 | +# Coprime elements of a ring |
| 14 | +
|
| 15 | +## Main definitions |
| 16 | +
|
| 17 | +* `is_coprime x y`: that `x` and `y` are coprime, defined to be the existence of `a` and `b` such |
| 18 | +that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, |
| 19 | +e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. |
| 20 | +
|
| 21 | +-/ |
| 22 | + |
| 23 | +open_locale classical big_operators |
| 24 | + |
| 25 | +universes u v |
| 26 | + |
| 27 | +variables {R : Type u} [comm_semiring R] (x y z : R) |
| 28 | + |
| 29 | +/-- The proposition that `x` and `y` are coprime, defined to be the existence of `a` and `b` such |
| 30 | +that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, |
| 31 | +e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. -/ |
| 32 | +@[simp] def is_coprime : Prop := |
| 33 | +∃ a b, a * x + b * y = 1 |
| 34 | + |
| 35 | +theorem nat.is_coprime_iff_coprime {m n : ℕ} : is_coprime (m : ℤ) n ↔ nat.coprime m n := |
| 36 | +⟨λ ⟨a, b, H⟩, nat.eq_one_of_dvd_one $ int.coe_nat_dvd.1 $ by { rw [int.coe_nat_one, ← H], |
| 37 | + exact dvd_add (dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_left m n) _) |
| 38 | + (dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_right m n) _) }, |
| 39 | +λ H, ⟨nat.gcd_a m n, nat.gcd_b m n, by rw [mul_comm _ (m : ℤ), mul_comm _ (n : ℤ), |
| 40 | + ← nat.gcd_eq_gcd_ab, show _ = _, from H, int.coe_nat_one]⟩⟩ |
| 41 | + |
| 42 | +variables {x y z} |
| 43 | + |
| 44 | +theorem is_coprime.symm (H : is_coprime x y) : is_coprime y x := |
| 45 | +let ⟨a, b, H⟩ := H in ⟨b, a, by rw [add_comm, H]⟩ |
| 46 | + |
| 47 | +theorem is_coprime_comm : is_coprime x y ↔ is_coprime y x := |
| 48 | +⟨is_coprime.symm, is_coprime.symm⟩ |
| 49 | + |
| 50 | +theorem is_coprime_self : is_coprime x x ↔ is_unit x := |
| 51 | +⟨λ ⟨a, b, h⟩, is_unit_of_mul_eq_one x (a + b) $ by rwa [mul_comm, add_mul], |
| 52 | +λ h, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 h in ⟨b, 0, by rwa [zero_mul, add_zero]⟩⟩ |
| 53 | + |
| 54 | +theorem is_coprime_zero_left : is_coprime 0 x ↔ is_unit x := |
| 55 | +⟨λ ⟨a, b, H⟩, is_unit_of_mul_eq_one x b $ by rwa [mul_zero, zero_add, mul_comm] at H, |
| 56 | +λ H, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 H in ⟨1, b, by rwa [one_mul, zero_add]⟩⟩ |
| 57 | + |
| 58 | +theorem is_coprime_zero_right : is_coprime x 0 ↔ is_unit x := |
| 59 | +is_coprime_comm.trans is_coprime_zero_left |
| 60 | + |
| 61 | +theorem is_coprime_one_left : is_coprime 1 x := |
| 62 | +⟨1, 0, by rw [one_mul, zero_mul, add_zero]⟩ |
| 63 | + |
| 64 | +theorem is_coprime_one_right : is_coprime x 1 := |
| 65 | +⟨0, 1, by rw [one_mul, zero_mul, zero_add]⟩ |
| 66 | + |
| 67 | +theorem is_coprime.dvd_of_dvd_mul_right (H1 : is_coprime x z) (H2 : x ∣ y * z) : x ∣ y := |
| 68 | +let ⟨a, b, H⟩ := H1 in by { rw [← mul_one y, ← H, mul_add, ← mul_assoc, mul_left_comm], |
| 69 | +exact dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_right H2 _) } |
| 70 | + |
| 71 | +theorem is_coprime.dvd_of_dvd_mul_left (H1 : is_coprime x y) (H2 : x ∣ y * z) : x ∣ z := |
| 72 | +let ⟨a, b, H⟩ := H1 in by { rw [← one_mul z, ← H, add_mul, mul_right_comm, mul_assoc b], |
| 73 | +exact dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_right H2 _) } |
| 74 | + |
| 75 | +theorem is_coprime.mul_left (H1 : is_coprime x z) (H2 : is_coprime y z) : is_coprime (x * y) z := |
| 76 | +let ⟨a, b, h1⟩ := H1, ⟨c, d, h2⟩ := H2 in |
| 77 | +⟨a * c, a * x * d + b * c * y + b * d * z, |
| 78 | +calc a * c * (x * y) + (a * x * d + b * c * y + b * d * z) * z |
| 79 | + = (a * x + b * z) * (c * y + d * z) : by ring |
| 80 | +... = 1 : by rw [h1, h2, mul_one]⟩ |
| 81 | + |
| 82 | +theorem is_coprime.mul_right (H1 : is_coprime x y) (H2 : is_coprime x z) : is_coprime x (y * z) := |
| 83 | +by { rw is_coprime_comm at H1 H2 ⊢, exact H1.mul_left H2 } |
| 84 | + |
| 85 | +variables {I : Type v} {s : I → R} {t : finset I} |
| 86 | + |
| 87 | +theorem is_coprime.prod_left : (∀ i ∈ t, is_coprime (s i) x) → is_coprime (∏ i in t, s i) x := |
| 88 | +finset.induction_on t (λ _, is_coprime_one_left) $ λ b t hbt ih H, |
| 89 | +by { rw finset.prod_insert hbt, rw finset.forall_mem_insert at H, exact H.1.mul_left (ih H.2) } |
| 90 | + |
| 91 | +theorem is_coprime.prod_right : (∀ i ∈ t, is_coprime x (s i)) → is_coprime x (∏ i in t, s i) := |
| 92 | +by simpa only [is_coprime_comm] using is_coprime.prod_left |
| 93 | + |
| 94 | +theorem is_coprime.mul_dvd (H : is_coprime x y) (H1 : x ∣ z) (H2 : y ∣ z) : x * y ∣ z := |
| 95 | +begin |
| 96 | + obtain ⟨a, b, h⟩ := H, |
| 97 | + rw [← mul_one z, ← h, mul_add], |
| 98 | + apply dvd_add, |
| 99 | + { rw [mul_comm z, mul_assoc], |
| 100 | + exact dvd_mul_of_dvd_right (mul_dvd_mul_left _ H2) _ }, |
| 101 | + { rw [mul_comm b, ← mul_assoc], |
| 102 | + exact dvd_mul_of_dvd_left (mul_dvd_mul_right H1 _) _ } |
| 103 | +end |
| 104 | + |
| 105 | +theorem finset.prod_dvd_of_coprime (Hs : pairwise (is_coprime on s)) (Hs1 : ∀ i, s i ∣ z) : |
| 106 | + ∏ x in t, s x ∣ z := |
| 107 | +finset.induction_on t (one_dvd z) (λ a r har ih, by { rw finset.prod_insert har, |
| 108 | +exact (is_coprime.prod_right $ λ i hir, Hs a i $ λ hai, har $ hai.symm ▸ hir).mul_dvd (Hs1 a) ih }) |
| 109 | + |
| 110 | +theorem fintype.prod_dvd_of_coprime [fintype I] (Hs : pairwise (is_coprime on s)) |
| 111 | + (Hs1 : ∀ i, s i ∣ z) : ∏ x, s x ∣ z := |
| 112 | +finset.prod_dvd_of_coprime Hs Hs1 |
| 113 | + |
| 114 | +theorem is_coprime.of_mul_left_left (H : is_coprime (x * y) z) : is_coprime x z := |
| 115 | +let ⟨a, b, h⟩ := H in ⟨a * y, b, by rwa [mul_right_comm, mul_assoc]⟩ |
| 116 | + |
| 117 | +theorem is_coprime.of_mul_left_right (H : is_coprime (x * y) z) : is_coprime y z := |
| 118 | +by { rw mul_comm at H, exact H.of_mul_left_left } |
| 119 | + |
| 120 | +theorem is_coprime.of_mul_right_left (H : is_coprime x (y * z)) : is_coprime x y := |
| 121 | +by { rw is_coprime_comm at H ⊢, exact H.of_mul_left_left } |
| 122 | + |
| 123 | +theorem is_coprime.of_mul_right_right (H : is_coprime x (y * z)) : is_coprime x z := |
| 124 | +by { rw mul_comm at H, exact H.of_mul_right_left } |
| 125 | + |
| 126 | +theorem is_coprime.mul_left_iff : is_coprime (x * y) z ↔ is_coprime x z ∧ is_coprime y z := |
| 127 | +⟨λ H, ⟨H.of_mul_left_left, H.of_mul_left_right⟩, λ ⟨H1, H2⟩, H1.mul_left H2⟩ |
| 128 | + |
| 129 | +theorem is_coprime.mul_right_iff : is_coprime x (y * z) ↔ is_coprime x y ∧ is_coprime x z := |
| 130 | +by rw [is_coprime_comm, is_coprime.mul_left_iff, is_coprime_comm, @is_coprime_comm _ _ z] |
| 131 | + |
| 132 | +theorem is_coprime.prod_left_iff : is_coprime (∏ i in t, s i) x ↔ ∀ i ∈ t, is_coprime (s i) x := |
| 133 | +finset.induction_on t (iff_of_true is_coprime_one_left $ λ _, false.elim) $ λ b t hbt ih, |
| 134 | +by rw [finset.prod_insert hbt, is_coprime.mul_left_iff, ih, finset.forall_mem_insert] |
| 135 | + |
| 136 | +theorem is_coprime.prod_right_iff : is_coprime x (∏ i in t, s i) ↔ ∀ i ∈ t, is_coprime x (s i) := |
| 137 | +by simpa only [is_coprime_comm] using is_coprime.prod_left_iff |
| 138 | + |
| 139 | +theorem is_coprime.of_prod_left (H1 : is_coprime (∏ i in t, s i) x) (i : I) (hit : i ∈ t) : |
| 140 | + is_coprime (s i) x := |
| 141 | +is_coprime.prod_left_iff.1 H1 i hit |
| 142 | + |
| 143 | +theorem is_coprime.of_prod_right (H1 : is_coprime x (∏ i in t, s i)) (i : I) (hit : i ∈ t) : |
| 144 | + is_coprime x (s i) := |
| 145 | +is_coprime.prod_right_iff.1 H1 i hit |
| 146 | + |
| 147 | +variables {m n : ℕ} |
| 148 | + |
| 149 | +theorem is_coprime.pow_left (H : is_coprime x y) : is_coprime (x ^ m) y := |
| 150 | +by { rw [← finset.card_range m, ← finset.prod_const], exact is_coprime.prod_left (λ _ _, H) } |
| 151 | + |
| 152 | +theorem is_coprime.pow_right (H : is_coprime x y) : is_coprime x (y ^ n) := |
| 153 | +by { rw [← finset.card_range n, ← finset.prod_const], exact is_coprime.prod_right (λ _ _, H) } |
| 154 | + |
| 155 | +theorem is_coprime.pow (H : is_coprime x y) : is_coprime (x ^ m) (y ^ n) := |
| 156 | +H.pow_left.pow_right |
| 157 | + |
| 158 | +theorem is_coprime.is_unit_of_dvd (H : is_coprime x y) (d : x ∣ y) : is_unit x := |
| 159 | +let ⟨k, hk⟩ := d in is_coprime_self.1 $ is_coprime.of_mul_right_left $ |
| 160 | +show is_coprime x (x * k), from hk ▸ H |
| 161 | + |
| 162 | +theorem is_coprime.map (H : is_coprime x y) {S : Type v} [comm_semiring S] (f : R →+* S) : |
| 163 | + is_coprime (f x) (f y) := |
| 164 | +let ⟨a, b, h⟩ := H in ⟨f a, f b, by rw [← f.map_mul, ← f.map_mul, ← f.map_add, h, f.map_one]⟩ |
0 commit comments