/-Copyright (c) 2026 Carles MarΓn. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles MarΓn, Claude (AI assistant)-/module public import Mathlib.FieldTheory.Finite.Basicpublic import Mathlib.Algebra.Polynomial.Monicpublic import Mathlib.Algebra.Polynomial.FieldDivisionpublic import Mathlib.RingTheory.Polynomial.Basicpublic import Mathlib.RingTheory.Polynomial.Contentpublic import Mathlib.SetTheory.Cardinal.Finitepublic import Mathlib.Algebra.BigOperators.Finpublic import Mathlib.Algebra.GCDMonoid.Finset /-!# The polynomial coprimality theorem over finite fields Over a finite field with `q` elements, the number of `m`-tuples of monic polynomials of degrees`nβ, β¦, n_m β₯ 1` with no non-unit common divisor is `q^N β q^(N β m + 1)`, `N = Ξ£ nα΅’`; with equaldegrees, the proportion of tuples with a non-trivial common factor is `1/q^(mβ1)` (Theorem of theDay). Proof: every tuple factors uniquely as `d Β· (gβ, β¦, g_m)` with `d` the monic gcd and the `gα΅’`coprime, so `q^N = Ξ£_k q^k Β· C(n β k)`, and strong induction on `min nα΅’` solves for `C`. ## Main results * `PolynomialCoprimality.card_monic_natDegree`: there are q^n monic polynomials of degree n* `PolynomialCoprimality.card_coprime_tuples`: coprime m-tuples of monic polynomials of degrees nα΅’ β₯ 1: q^N β q^(Nβm+1)* `PolynomialCoprimality.card_not_coprime_equal_degree`: equal degrees: q^(mn β m + 1) tuples share a non-trivial factor (proportion 1/q^(mβ1)) ## References * R. Whitty, Theorem of the Day, Β«The Polynomial Coprimality TheoremΒ» (CombinatorialTheory/Coprimality).* S. Corteel, C. D. Savage, H. S. Wilf, D. Zeilberger, A pentagonal number sieve, J. Combin. Theory Ser. A 82 (1998).* A. T. Benjamin, C. D. Bennett, The probability of relatively prime polynomials, Math. Mag. 80 (2007).-/ @[expose] public section open Polynomial namespace PolynomialCoprimality variable (K : Type*) [Field K] /-- Monic polynomials of natural degree `n`. -/abbrev MonicDeg (n : β) := {f : K[X] // f.Monic β§ f.natDegree = n} noncomputable def monicEquiv (n : β) : MonicDeg K n β degreeLT K n where toFun f := β¨f.1 - X ^ n, by rw [mem_degreeLT] have h1 : f.1.degree = (n : WithBot β) := by rw [degree_eq_natDegree f.2.1.ne_zero, f.2.2] have h2 : (X ^ n : K[X]).degree = n := degree_X_pow n have := degree_sub_lt_left (p := f.1) (q := X ^ n) (h1.trans h2.symm) f.2.1.ne_zero (by rw [f.2.1.leadingCoeff, leadingCoeff_X_pow]) rwa [h1] at thisβ© invFun g := β¨X ^ n + g.1, monic_X_pow_add (mem_degreeLT.1 g.2), by rw [natDegree_add_eq_left_of_degree_lt, natDegree_X_pow] rw [degree_X_pow]; exact mem_degreeLT.1 g.2β© left_inv f := Subtype.ext (by simp) right_inv g := Subtype.ext (by simp) theorem card_monic_natDegree [Fintype K] (n : β) : Nat.card {f : K[X] // f.Monic β§ f.natDegree = n} = Fintype.card K ^ n := by rw [Nat.card_congr ((monicEquiv K n).trans (degreeLTEquiv K n).toEquiv)] simp instance finMonicDeg [Finite K] (n : β) : Finite (MonicDeg K n) := Finite.of_equiv _ ((monicEquiv K n).trans (degreeLTEquiv K n).toEquiv).symm /-- Tuples of monic polynomials with prescribed degrees. -/abbrev TupDeg (m : β) (n : Fin m β β) := {t : Fin m β K[X] // β i, (t i).Monic β§ (t i).natDegree = n i} /-- Such tuples with no non-unit common divisor. -/abbrev CopDeg (m : β) (n : Fin m β β) := {t : Fin m β K[X] // (β i, (t i).Monic β§ (t i).natDegree = n i) β§ β d : K[X], (β i, d β£ t i) β IsUnit d} variable {K} noncomputable def tupEquiv {m : β} (n : Fin m β β) : TupDeg K m n β β i, MonicDeg K (n i) := Equiv.subtypePiEquivPi (p := fun i (f : K[X]) => f.Monic β§ f.natDegree = n i) instance finTup [Finite K] {m : β} (n : Fin m β β) : Finite (TupDeg K m n) := Finite.of_equiv _ (tupEquiv n).symm instance finCop [Finite K] {m : β} (n : Fin m β β) : Finite (CopDeg K m n) := Finite.of_injective (fun t : CopDeg K m n => (β¨t.1, t.2.1β© : TupDeg K m n)) (fun a b h => Subtype.ext (by simpa using congrArg Subtype.val h)) theorem card_tup [Fintype K] {m : β} (n : Fin m β β) : Nat.card (TupDeg K m n) = Fintype.card K ^ (β i, n i) := by rw [Nat.card_congr (tupEquiv n), Nat.card_pi] simp [card_monic_natDegree, Finset.prod_pow_eq_pow_sum] /-- A normalized gcd on `K[X]`, local to this file (Mathlib has no global instance for a bare field). -/noncomputable local instance instNGCD [DecidableEq K] : NormalizedGCDMonoid K[X] := UniqueFactorizationMonoid.toNormalizedGCDMonoid _ theorem gcd_mul_cop [DecidableEq K] {m : β} (d : K[X]) (hd : d.Monic) (g : Fin m β K[X]) (hg : β c : K[X], (β i, c β£ g i) β IsUnit c) : Finset.univ.gcd (fun i => d * g i) = d := by have h1 : Finset.univ.gcd g = 1 := by rw [β Finset.normalize_gcd, normalize_eq_one] exact hg _ (fun i => Finset.gcd_dvd (Finset.mem_univ i)) have h2 := Finset.gcd_mul_left' Finset.univ g d rw [h1, mul_one] at h2 calc _ = normalize (Finset.univ.gcd fun i => d * g i) := (Finset.normalize_gcd ..).symm _ = normalize d := normalize_eq_normalize h2.dvd h2.symm.dvd _ = d := hd.normalize_eq_self theorem exists_decomp [DecidableEq K] {m : β} (hm : 0 < m) (t : Fin m β K[X]) (ht : β i, (t i).Monic) : β (d : K[X]) (s : Fin m β K[X]), d.Monic β§ (β i, (s i).Monic) β§ (β i, t i = d * s i) β§ β c : K[X], (β i, c β£ s i) β IsUnit c := by obtain β¨s, hs, hgβ© := Finset.extract_gcd t (Finset.univ_nonempty_iff.2 β¨β¨0, hmβ©β©) obtain β¨d, hdβ© : β d, d = Finset.univ.gcd t := β¨_, rflβ© rw [β hd] at hs have hd0 : d β 0 := by intro h0 have := hs β¨0, hmβ© (Finset.mem_univ _) rw [h0, zero_mul] at this exact (ht _).ne_zero this have hdm : d.Monic := by have := monic_normalize hd0 rwa [hd, Finset.normalize_gcd, β hd] at this refine β¨d, s, hdm, fun i => hdm.of_mul_monic_left (by rw [β hs i (Finset.mem_univ i)]; exact ht i), fun i => hs i (Finset.mem_univ i), fun c hc => ?_β© have := Finset.dvd_gcd (s := Finset.univ) (f := s) (fun i _ => hc i) rw [hg] at this exact isUnit_of_dvd_one this /-- Decomposition data: a monic `d` of degree `k β€ L` and a coprime tuple of degrees `nα΅’ - k`. -/abbrev Dec (K : Type*) [Field K] (m : β) (n : Fin m β β) (L : β) := Ξ£ k : Fin (L + 1), MonicDeg K k Γ CopDeg K m (fun i => n i - k) theorem decMap_prop {m : β} (n : Fin m β β) (L : β) (hL : β i, L β€ n i) (x : Dec K m n L) (i : Fin m) : (x.2.1.1 * x.2.2.1 i).Monic β§ (x.2.1.1 * x.2.2.1 i).natDegree = n i := by obtain β¨k, β¨d, hd, hdkβ©, β¨g, hg, -β©β© := x have hk : (k : β) β€ n i := le_trans (Nat.lt_succ_iff.1 k.2) (hL i) show (d * g i).Monic β§ (d * g i).natDegree = n i refine β¨hd.mul (hg i).1, ?_β© rw [hd.natDegree_mul (hg i).1, hdk, (hg i).2] show (k : β) + (n i - k) = n i omega /-- The multiplication map `(d, g) β¦ d β’ g`. -/noncomputable def decMap {m : β} (n : Fin m β β) (L : β) (hL : β i, L β€ n i) : Dec K m n L β TupDeg K m n := fun x => β¨fun i => x.2.1.1 * x.2.2.1 i, decMap_prop n L hL xβ© theorem decMap_bijective [DecidableEq K] {m : β} (hm : 0 < m) (n : Fin m β β) (L : β) (hL : β i, L β€ n i) (hL' : β i, n i = L) : Function.Bijective (decMap (K := K) n L hL) := by constructor Β· intro x y h obtain β¨k, β¨d, hd, hdkβ©, β¨g, hg, hgcβ©β© := x obtain β¨k', β¨d', hd', hdk'β©, β¨g', hg', hgc'β©β© := y have h' : β i, d * g i = d' * g' i := fun i => congrFun (congrArg Subtype.val h) i have hfun : (fun i => d * g i) = fun i => d' * g' i := funext h' have e1 : d = d' := calc d = Finset.univ.gcd (fun i => d * g i) := (gcd_mul_cop d hd g hgc).symm _ = Finset.univ.gcd (fun i => d' * g' i) := by rw [hfun] _ = d' := gcd_mul_cop d' hd' g' hgc' subst e1 have hk : k = k' := Fin.ext (hdk.symm.trans hdk') subst hk have hgg : g = g' := funext fun i => mul_left_cancelβ hd.ne_zero (h' i) subst hgg rfl Β· intro t obtain β¨d, s, hd, hs, hts, hcopβ© := exists_decomp hm t.1 (fun i => (t.2 i).1) have hdn : β i, d.natDegree + (s i).natDegree = n i := fun i => by rw [β hd.natDegree_mul (hs i), β hts i]; exact (t.2 i).2 obtain β¨i0, hi0β© := hL' have hdL : d.natDegree β€ L := by have := hdn i0; omega refine β¨β¨β¨d.natDegree, Nat.lt_succ_of_le hdLβ©, β¨d, hd, rflβ©, β¨s, fun i => β¨hs i, ?_β©, hcopβ©β©, ?_β© Β· show (s i).natDegree = n i - d.natDegree have := hdn i; omega Β· apply Subtype.ext; funext i; exact (hts i).symm /-- `q ^ N = β_{k β€ L} q ^ k * C(n - k)`. -/theorem card_decomp [Fintype K] [DecidableEq K] {m : β} (hm : 0 < m) (n : Fin m β β) (L : β) (hL : β i, L β€ n i) (hL' : β i, n i = L) : Fintype.card K ^ (β i, n i) = β k β Finset.range (L + 1), Fintype.card K ^ k * Nat.card (CopDeg K m (fun i => n i - k)) := by rw [β card_tup n, β Nat.card_congr (Equiv.ofBijective _ (decMap_bijective hm n L hL hL')), Nat.card_sigma, β Fin.sum_univ_eq_sum_range (fun k => Fintype.card K ^ k * Nat.card (CopDeg K m (fun i => n i - k))) (L + 1)] refine Finset.sum_congr rfl (fun k _ => ?_) rw [Nat.card_prod, card_monic_natDegree] /-- Pure telescoping step. -/theorem telescope (f a : β β β) (L : β) (hL : 1 β€ L) (h1 : β j, 1 β€ j β j < L β f j + a (j + 1) = a j) (h2 : f L = a L) : β k β Finset.range L, f (k + 1) = a 1 := by have key : β t, t < L β β k β Finset.range t, f (k + 1) + a (t + 1) = a 1 := by intro t induction t with | zero => intro _; simp | succ t ih => intro ht have := ih (by omega) have h := h1 (t + 1) (by omega) ht rw [Finset.sum_range_succ] omega obtain β¨t, rflβ© : β t, L = t + 1 := β¨L - 1, by omegaβ© rw [Finset.sum_range_succ, h2] exact key t (by omega) theorem count_ind [Fintype K] [DecidableEq K] {m : β} (hm : 0 < m) : β N : β, β n : Fin m β β, β i, n i = N β ((β i, n i = 0) β Nat.card (CopDeg K m n) = Fintype.card K ^ N) β§ ((β i, 1 β€ n i) β Nat.card (CopDeg K m n) + Fintype.card K ^ (N - m + 1) = Fintype.card K ^ N) := by intro N induction N using Nat.strong_induction_on with | _ N ih => intro n hN constructor Β· rintro β¨i0, hi0β© have := card_decomp (K := K) hm n 0 (fun i => Nat.zero_le _) β¨i0, hi0β© simpa [hN] using this.symm Β· intro hpos obtain β¨i0, -, hi0β© := Finset.exists_min_image Finset.univ n β¨β¨0, hmβ©, Finset.mem_univ _β© have hL : β i, n i0 β€ n i := fun i => hi0 i (Finset.mem_univ i) have hL1 : 1 β€ n i0 := hpos i0 have hdec := card_decomp (K := K) hm n (n i0) hL β¨i0, rflβ© rw [hN, Finset.sum_range_succ'] at hdec simp only [Nat.sub_zero, pow_zero, one_mul] at hdec have hmN : m β€ N := by have := Finset.sum_le_sum (s := Finset.univ) (f := fun _ : Fin m => 1) (g := n) (fun i _ => hpos i) simp at this; omega have hsum : β k β Finset.range (n i0), Fintype.card K ^ (k + 1) * Nat.card (CopDeg K m (fun i => n i - (k + 1))) = Fintype.card K ^ (N - m + 1) := by have hmain := telescope (fun j => Fintype.card K ^ j * Nat.card (CopDeg K m (fun i => n i - j))) (fun j => Fintype.card K ^ (N + j - m * j)) (n i0) hL1 ?_ ?_ Β· have e : N + 1 - m * 1 = N - m + 1 := by omega simp only [e] at hmain exact hmain Β· intro j hj1 hjL have hj : β i, j β€ n i := fun i => by have := hL i; omega have hs : β i, (n i - j) = N - m * j := by rw [Finset.sum_tsub_distrib _ (fun i _ => hj i), hN]; simp have hmj : m * j β€ N := by have := Finset.sum_le_sum (s := Finset.univ) (f := fun _ : Fin m => j) (g := n) (fun i _ => hj i) simp at this; omega have hge : m β€ N - m * j := by have := Finset.sum_le_sum (s := Finset.univ) (f := fun _ : Fin m => 1) (g := fun i => n i - j) (fun i _ => by have := hL i; omega) simp at this; omega have hpos' : 0 < m * j := Nat.mul_pos hm hj1 have := (ih (N - m * j) (by omega) (fun i => n i - j) hs).2 (fun i => by have := hL i; omega) have h3 := congrArg (fun x => Fintype.card K ^ j * x) this simp only [mul_add, β pow_add] at h3 have e1 : j + (N - m * j - m + 1) = N + (j + 1) - m * (j + 1) := by rw [Nat.mul_succ]; omega have e2 : j + (N - m * j) = N + j - m * j := by omega rw [e1, e2] at h3 exact h3 Β· have hj : β i, n i0 β€ n i := hL have hs : β i, (n i - n i0) = N - m * n i0 := by rw [Finset.sum_tsub_distrib _ (fun i _ => hj i), hN]; simp have hmj : m * n i0 β€ N := by have := Finset.sum_le_sum (s := Finset.univ) (f := fun _ : Fin m => n i0) (g := n) (fun i _ => hj i) simp at this; omega have hpos' : 0 < m * n i0 := Nat.mul_pos hm hL1 have := (ih (N - m * n i0) (by omega) (fun i => n i - n i0) hs).1 β¨i0, by simpβ© show Fintype.card K ^ n i0 * Nat.card (CopDeg K m (fun i => n i - n i0)) = Fintype.card K ^ (N + n i0 - m * n i0) rw [this, β pow_add] congr 1; omega rw [hsum] at hdec have e : Nat.card (CopDeg K m fun i => n i) = Nat.card (CopDeg K m n) := rfl rw [e] at hdec omega /-- **The polynomial coprimality theorem** (general degrees). -/theorem card_coprime_tuples (K : Type*) [Field K] [Fintype K] (m : β) (hm : 1 β€ m) (n : Fin m β β) (hn : β i, 1 β€ n i) : Nat.card {t : Fin m β K[X] // (β i, (t i).Monic β§ (t i).natDegree = n i) β§ β d : K[X], (β i, d β£ t i) β IsUnit d} = Fintype.card K ^ (β i, n i) - Fintype.card K ^ (β i, n i - m + 1) := by classical have h := (count_ind (K := K) hm (β i, n i) n rfl).2 hn show Nat.card (CopDeg K m n) = _ omega /-- Equal degrees: `q ^ (m * n - m + 1)` tuples of monic polynomials of degree `n` share anon-trivial common factor. -/theorem card_not_coprime_equal_degree (K : Type*) [Field K] [Fintype K] (m : β) (hm : 1 β€ m) (n : β) (hn : 1 β€ n) : Nat.card {t : Fin m β K[X] // (β i, (t i).Monic β§ (t i).natDegree = n) β§ β d : K[X], (β i, d β£ t i) β§ Β¬ IsUnit d} = Fintype.card K ^ (m * n - m + 1) := by classical have hsum : β _i : Fin m, n = m * n := by simp have h := (count_ind (K := K) hm (β _i : Fin m, n) (fun _ => n) rfl).2 (fun _ => hn) rw [hsum] at h have hcop : Nat.card (CopDeg K m (fun _ => n)) = Nat.card {x : TupDeg K m (fun _ => n) // β d : K[X], (β i, d β£ x.1 i) β IsUnit d} := Nat.card_congr { toFun := fun t => β¨β¨t.1, t.2.1β©, t.2.2β© invFun := fun x => β¨x.1.1, x.1.2, x.2β© left_inv := fun _ => rfl right_inv := fun _ => rfl } have e1 : {t : Fin m β K[X] // (β i, (t i).Monic β§ (t i).natDegree = n) β§ β d : K[X], (β i, d β£ t i) β§ Β¬ IsUnit d} β {x : TupDeg K m (fun _ => n) // Β¬ β d : K[X], (β i, d β£ x.1 i) β IsUnit d} := { toFun := fun t => β¨β¨t.1, t.2.1β©, by obtain β¨d, hd1, hd2β© := t.2.2 exact fun hc => hd2 (hc d hd1)β© invFun := fun x => β¨x.1.1, x.1.2, by have := x.2 push Not at this obtain β¨d, hd1, hd2β© := this exact β¨d, hd1, hd2β©β© left_inv := fun _ => rfl right_inv := fun _ => rfl } let _ : Fintype (TupDeg K m (fun _ => n)) := Fintype.ofFinite _ rw [Nat.card_congr e1, Nat.card_eq_fintype_card, Fintype.card_subtype_compl, β Nat.card_eq_fintype_card, β Nat.card_eq_fintype_card, β hcop, card_tup] simp only [hsum] omega end PolynomialCoprimality