Skip to content

Commit 555129f

Browse files
committed
chore(Data/Int/GCD): review API (#31140)
This PR makes a few changes, but they are very linked so a single PR seems easiest: - Rename `Nat.exists_mul_emod_eq_gcd` to `Nat.exists_mul_mod_eq_gcd`, and `Nat.exists_mul_emod_eq_one_of_coprime` to `Nat.exists_mul_mod_eq_one_of_coprime`. This is since the `%` operator on naturals is only ever named `mod`, whereas `emod` is reserved for the `%` operator on integers. - Strengthen the statements of those two to additionally deduce that `m < k`. - Add `exists_mul_mod_eq_of_coprime`, which is the non-one generalisation of `exists_mul_mod_eq_one_of_coprime`.
1 parent d7b5141 commit 555129f

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

Archive/Imo/Imo1985Q2.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ lemma C_mul_mod {n j : ℕ} (hn : 3 ≤ n) (hj : j ∈ Set.Ico 1 n) (cpj : Nat.C
7575
theorem result {n j : ℕ} (hn : 3 ≤ n) (hj : j ∈ Set.Ico 1 n) (cpj : Coprime n j)
7676
{C : ℕ → Fin 2} (hC : Condition n j C) {i : ℕ} (hi : i ∈ Set.Ico 1 n) :
7777
C i = C j := by
78-
obtain ⟨v, hv⟩ := exists_mul_emod_eq_one_of_coprime cpj.symm (by omega)
78+
obtain ⟨v, -, hv⟩ := exists_mul_mod_eq_one_of_coprime cpj.symm (by omega)
7979
have hvi : i = (v * i % n) * j % n := by
8080
rw [mod_mul_mod, ← mul_rotate, ← mod_mul_mod, hv, one_mul, mod_eq_of_lt hi.2]
8181
have vib : v * i % n ∈ Set.Ico 1 n := by

Mathlib/Data/Int/GCD.lean

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,32 @@ theorem gcd_eq_gcd_ab : (gcd x y : ℤ) = x * gcdA x y + y * gcdB x y := by
133133

134134
end
135135

136-
theorem exists_mul_emod_eq_gcd {k n : ℕ} (hk : gcd n k < k) : ∃ m, n * m % k = gcd n k := by
137-
have hk' := Int.ofNat_ne_zero.2 (ne_of_gt (lt_of_le_of_lt (zero_le (gcd n k)) hk))
138-
have key := congr_arg (fun (m : ℤ) => (m % k).toNat) (gcd_eq_gcd_ab n k)
139-
simp only at key
136+
theorem exists_mul_mod_eq_gcd {k n : ℕ} (hk : gcd n k < k) : ∃ m < k, n * m % k = gcd n k := by
137+
have hk' := Int.ofNat_ne_zero.2 (Nat.zero_lt_of_lt hk).ne'
138+
have key := congr(($(gcd_eq_gcd_ab n k) % k).toNat)
140139
rw [Int.add_mul_emod_self_left, ← Int.natCast_mod, Int.toNat_natCast, mod_eq_of_lt hk] at key
141-
refine ⟨(n.gcdA k % k).toNat, Eq.trans (Int.ofNat.inj ?_) key.symm⟩
142-
rw [Int.ofNat_eq_natCast, Int.natCast_mod, Int.natCast_mul,
143-
Int.toNat_of_nonneg (Int.emod_nonneg _ hk'), Int.ofNat_eq_natCast,
140+
refine ⟨(n.gcdA k % k).toNat, ?_, (Int.ofNat_inj.1 ?_).trans key.symm⟩
141+
· rw [Int.toNat_lt (Int.emod_nonneg _ hk')]
142+
exact Int.emod_lt _ hk'
143+
rw [Int.natCast_mod, Int.natCast_mul, Int.toNat_of_nonneg (Int.emod_nonneg _ hk'),
144144
Int.toNat_of_nonneg (Int.emod_nonneg _ hk'), Int.mul_emod, Int.emod_emod, ← Int.mul_emod]
145145

146-
theorem exists_mul_emod_eq_one_of_coprime {k n : ℕ} (hkn : Coprime n k) (hk : 1 < k) :
147-
∃ m, n * m % k = 1 :=
148-
Exists.recOn (exists_mul_emod_eq_gcd (lt_of_le_of_lt (le_of_eq hkn) hk)) fun m hm ↦
149-
⟨m, hm.trans hkn⟩
146+
@[deprecated (since := "2025-11-01")] alias exists_mul_emod_eq_gcd := exists_mul_mod_eq_gcd
147+
148+
theorem exists_mul_mod_eq_one_of_coprime {k n : ℕ} (hkn : Coprime n k) (hk : 1 < k) :
149+
∃ m < k, n * m % k = 1 := by
150+
simpa [hkn, hk] using exists_mul_mod_eq_gcd (k := k) (n := n)
151+
152+
@[deprecated (since := "2025-11-01")] alias exists_mul_emod_eq_one_of_coprime :=
153+
exists_mul_mod_eq_one_of_coprime
154+
155+
theorem exists_mul_mod_eq_of_coprime {k n : ℕ} (r : ℕ) (hkn : Coprime n k) (hk : k ≠ 0) :
156+
∃ m < k, n * m % k = r % k := by
157+
obtain rfl | hk : k = 11 < k := by omega
158+
next => simp [mod_one]
159+
obtain ⟨m, -, hm⟩ := exists_mul_mod_eq_one_of_coprime hkn hk
160+
use (m * r) % k, mod_lt _ (by omega)
161+
rw [mul_mod, mod_mod, ← mul_mod, ← mul_assoc, mul_mod, hm, one_mul, mod_mod]
150162

151163
end Nat
152164

Mathlib/GroupTheory/OrderOfElement.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ theorem exists_pow_eq_self_of_coprime (h : n.Coprime (orderOf x)) : ∃ m : ℕ,
302302
exact ⟨1, by rw [h, pow_one, pow_one]⟩
303303
by_cases h1 : orderOf x = 1
304304
· exact ⟨0, by rw [orderOf_eq_one_iff.mp h1, one_pow, one_pow]⟩
305-
obtain ⟨m, h⟩ := exists_mul_emod_eq_one_of_coprime h (one_lt_iff_ne_zero_and_ne_one.mpr ⟨h0, h1⟩)
305+
obtain ⟨m, -, h⟩ := exists_mul_mod_eq_one_of_coprime h (by omega)
306306
exact ⟨m, by rw [← pow_mul, ← pow_mod_orderOf, h, pow_one]⟩
307307

308308
/-- If `x^n = 1`, but `x^(n/p) ≠ 1` for all prime factors `p` of `n`,

Mathlib/RingTheory/RootsOfUnity/CyclotomicUnits.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ theorem associated_sub_one_pow_sub_one_of_coprime (hζ : IsPrimitiveRoot ζ n) (
5151
| 0 => simp_all
5252
| 1 => simp_all
5353
| n + 2 =>
54-
obtain ⟨m, hm⟩ := exists_mul_emod_eq_one_of_coprime hj (by omega)
54+
obtain ⟨m, -, hm⟩ := exists_mul_mod_eq_one_of_coprime hj (by omega)
5555
use ∑ i ∈ range m, (ζ ^ j) ^ i
5656
rw [mul_geom_sum, ← pow_mul, ← pow_mod_orderOf, ← hζ.eq_orderOf, hm, pow_one]
5757

0 commit comments

Comments
 (0)