/- Copyright (c) 2026 Carl McTague. All rights reserved. Released under Apache 2.0 license. Authors: Carl McTague, autoformalized from .tex file by Qwen3.8:27 using 19gb of vram on a Framework Desktop PC -/ import Mathlib.Tactic import Mathlib.Data.Nat.Choose.Lucas import Mathlib.Data.Nat.Choose.Vandermonde import Mathlib.Data.Nat.Choose.Factorization import Mathlib.Data.Nat.GCD.Basic import Mathlib.NumberTheory.Padics.PadicVal.Basic import Mathlib.Data.Nat.Digits.Lemmas /-! # On the Greatest Common Divisor of Binomial Coefficients Main result: Theorem Q. For n > q > 0 and prime p congruent to 1 mod q: ord_p [ gcd_{0 0 < k ∧ q * k < n) /- The binomial GCD: gcd_{0 < k < n/q} C(n, qk). -/ noncomputable def binomialGcd (n q : ℕ) : ℕ := (binomialIndices n q).gcd (fun k => Nat.choose n (q * k)) theorem binomialIndices_nonempty (n q : ℕ) (hq : 0 < q) (hn : q < n) : (binomialIndices n q).Nonempty := by have h1n : 1 < n := by omega exact ⟨1, by simp [binomialIndices, h1n, hn]⟩ theorem binomialGcd_dvd_all (n q : ℕ) (k : ℕ) (h : k ∈ binomialIndices n q) : binomialGcd n q ∣ Nat.choose n (q * k) := by show (binomialIndices n q).gcd (fun k => Nat.choose n (q * k)) ∣ _ exact Finset.gcd_dvd h theorem binomialGcd_pos (n q : ℕ) (hq : 0 < q) (hn : q < n) : 0 < binomialGcd n q := by obtain ⟨k, hk⟩ := binomialIndices_nonempty n q hq hn have hdiv : binomialGcd n q ∣ Nat.choose n (q * k) := binomialGcd_dvd_all n q k hk have hk2 : q * k ≤ n := by have hk3 := Finset.mem_filter.1 hk omega have hpos : 0 < Nat.choose n (q * k) := Nat.choose_pos hk2 exact Nat.pos_of_dvd_of_pos hdiv hpos /-! ### Helper lemma for the `r > q` branch of Theorem Q Write `n = p ^ i_1 + ⋯ + p ^ i_r` with `i_1 ≤ ⋯ ≤ i_r` and `r = α_p(n)`. The proof of Theorem Q for `r > q` uses two facts: that `p ^ i_1 + ⋯ + p ^ i_q` is divisible by `q` (this file), and that it is a strict sub-`p`-sum of `n` so that `ord_p [{n \choose p ^ i_1 + ⋯ + p ^ i_q}] = 0` by Lemma `subseq` / Kummer. -/ /-- If `p ≡ 1 mod q` and `2 ≤ q`, then every power of `p` is congruent to `1` modulo `q`. -/ theorem pow_mod_eq_one_of_mod_one {p q i : ℕ} (hcon : p % q = 1) (hq2 : 2 ≤ q) : p ^ i % q = 1 := by rw [Nat.pow_mod, hcon, one_pow] exact Nat.mod_eq_of_lt (by omega) /-- A number congruent to `1` modulo `q` is of the form `(m / q) * q + 1`. -/ theorem mod_add_div_one {m q : ℕ} (hm : m % q = 1) : m = (m / q) * q + 1 := by have h := Nat.mod_add_div' m q rw [hm] at h omega /-- **Helper lemma for the `r > q` branch.** If `p ≡ 1 mod q`, then a sum of exactly `q` powers of `p` is divisible by `q`. This is the divisibility step in the sentence "since `p` is congruent to `1` modulo `q`, so is each power `p ^ i`, so `p ^ i_1 + ⋯ + p ^ i_q` is divisible by `q`". -/ theorem sum_pow_dvd_of_mod_one {p q : ℕ} (hq : 0 < q) (hcon : p % q = 1) (a : ℕ → ℕ) : q ∣ ∑ i ∈ Finset.range q, p ^ a i := by by_cases hq2 : 2 ≤ q · have hterm : ∀ i, p ^ a i = (p ^ a i / q) * q + 1 := by intro i exact mod_add_div_one (pow_mod_eq_one_of_mod_one (i := a i) hcon hq2) obtain ⟨u, hu⟩ : ∃ u : ℕ → ℕ, ∀ i, p ^ a i = u i * q + 1 := by exact ⟨fun i => p ^ a i / q, fun i => hterm i⟩ have hcard : (∑ _i ∈ Finset.range q, (1 : ℕ)) = q := by simp have hmul : (∑ i ∈ Finset.range q, u i * q) = (∑ i ∈ Finset.range q, u i) * q := (Finset.sum_mul (Finset.range q) u q).symm refine ⟨∑ i ∈ Finset.range q, u i + 1, ?_⟩ simp_rw [hu, Finset.sum_add_distrib, hmul, hcard] ring · have hq1 : q = 1 := by omega simp [hq1] /-- The digit at position `0` of `m` in base `p` is `m % p`. -/ theorem digits_getD_zero {p m : ℕ} (hp : 2 ≤ p) : (Nat.digits p m).getD 0 0 = m % p := by rw [Nat.getD_digits m 0 (Nat.lt_of_lt_of_le hp (by omega)), Nat.pow_zero, Nat.div_one] /-- The digit at position `i + 1` of `m` in base `p` is the digit at position `i` of `m / p`. -/ theorem digits_getD_succ {p m : ℕ} (hp : 2 ≤ p) (hm : m ≠ 0) (i : ℕ) : (Nat.digits p m).getD (i + 1) 0 = (Nat.digits p (m / p)).getD i 0 := by rw [Nat.digits_eq_cons_digits_div (Nat.lt_of_lt_of_le hp (by omega)) hm, List.getD_cons_succ] /-- **Divisibility criterion for binomial coefficients.** If `p` is prime and `m ≤ n`, then `p ∣ n.choose m` if and only if some base-`p` digit of `m` exceeds the corresponding digit of `n` (Kummer's / Lucas' theorem). -/ theorem dvd_choose_iff_digits_gt {p m n : ℕ} (hp : p.Prime) (hmn : m ≤ n) : p ∣ Nat.choose n m ↔ ∃ i, (Nat.digits p m).getD i 0 > (Nat.digits p n).getD i 0 := by have hp2 : 2 ≤ p := hp.two_le by_cases hm0 : m = 0 · subst hm0 refine Iff.intro ?_ ?_ · intro h have h1 : Nat.choose n 0 = 1 := Nat.choose_zero_right n rw [h1] at h exact absurd h hp.not_dvd_one · rintro ⟨i, hi⟩ simp only [Nat.digits_zero, List.getD_nil] at hi omega induction n using Nat.strong_induction_on generalizing m with | _ n ih => have hn0 : n ≠ 0 := by omega have hdiv : n / p < n := Nat.div_lt_self (by omega) (by omega) have hluc : Nat.choose n m ≡ (n % p).choose (m % p) * (n / p).choose (m / p) [MOD p] := by letI : Fact (Nat.Prime p) := ⟨hp⟩ exact Choose.choose_modEq_choose_mod_mul_choose_div_nat have hrec (hz : m / p ≠ 0) : p ∣ Nat.choose (n / p) (m / p) ↔ ∃ i, (Nat.digits p (m / p)).getD i 0 > (Nat.digits p (n / p)).getD i 0 := ih (n / p) hdiv (Nat.div_le_div_right hmn) hz constructor · intro h have hz : Nat.choose n m ≡ 0 [MOD p] := Nat.modEq_zero_iff_dvd.mpr h have hprod : p ∣ (n % p).choose (m % p) * (n / p).choose (m / p) := Nat.modEq_zero_iff_dvd.mp (Nat.ModEq.trans (Nat.ModEq.symm hluc) hz) rcases (Nat.Prime.dvd_mul hp).mp hprod with hlow | hhigh · by_cases hle : m % p ≤ n % p · exfalso have h1 := (Nat.Prime.dvd_iff_one_le_factorization hp (Nat.choose_ne_zero (n := n % p) (k := m % p) hle)).mp hlow rw [Nat.factorization_choose_eq_zero_of_lt (p := p) (n := n % p) (k := m % p) (Nat.mod_lt n hp.pos)] at h1 omega · refine ⟨0, ?_⟩ have hmz := digits_getD_zero (m := m) hp2 have hnz := digits_getD_zero (m := n) hp2 omega · by_cases hmp0 : m / p = 0 · exfalso have hz' : p ∣ 1 := by rw [hmp0, Nat.choose_zero_right] at hhigh exact hhigh exact absurd hz' hp.not_dvd_one · obtain ⟨i, hi⟩ := (hrec hmp0).mp hhigh refine ⟨i + 1, ?_⟩ have h1 := digits_getD_succ hp2 hm0 i have h2 := digits_getD_succ hp2 hn0 i rw [h1, h2] exact hi · rintro ⟨i, hi⟩ rcases i with _ | i · have h1 : m % p > n % p := by have hmz := digits_getD_zero (m := m) hp2 have hnz := digits_getD_zero (m := n) hp2 omega have hz : (n % p).choose (m % p) * (n / p).choose (m / p) ≡ 0 [MOD p] := by rw [Nat.choose_eq_zero_of_lt h1, zero_mul] exact Nat.modEq_zero_iff_dvd.mp (Nat.ModEq.trans hluc hz) · have h1 := digits_getD_succ hp2 hm0 i have h2 := digits_getD_succ hp2 hn0 i have hh : (Nat.digits p (m / p)).getD i 0 > (Nat.digits p (n / p)).getD i 0 := by omega have hmp0 : m / p ≠ 0 := by intro hz have h4 := hh rw [hz, Nat.digits_zero, List.getD_nil] at h4 omega have hz : (n % p).choose (m % p) * (n / p).choose (m / p) ≡ 0 [MOD p] := Nat.ModEq.mul_left _ (Nat.modEq_zero_iff_dvd.mpr ((hrec hmp0).mpr ⟨i, hh⟩)) exact Nat.modEq_zero_iff_dvd.mp (Nat.ModEq.trans hluc hz) /-- Multiplying by `p` just appends a zero digit, so it does not change the digit sum. -/ theorem pDigitSum_mul_p {p x : ℕ} (hp : 2 ≤ p) : pDigitSum p (p * x) = pDigitSum p x := by by_cases hx : x = 0 · simp [hx, pDigitSum] · have hd := Nat.digits_base_pow_mul (b := p) (k := 1) (m := x) (Nat.lt_of_lt_of_le hp (by omega)) (Nat.pos_of_ne_zero hx) have hds : p.digits (p * x) = [0] ++ p.digits x := by simpa using hd simp [pDigitSum, hds] /-- **No carry in `m + (n - m)`.** If every base-`p` digit of `m` is at most the corresponding digit of `n`, then no carry occurs in `m + (n - m) = n`, so the base-`p` digit sums add up. -/ theorem digit_sum_add_sub_of_digits_le {p m n : ℕ} (hp : 2 ≤ p) (hmn : m ≤ n) (h : ∀ i, (Nat.digits p m).getD i 0 ≤ (Nat.digits p n).getD i 0) : pDigitSum p m + pDigitSum p (n - m) = pDigitSum p n := by have hp2 : 1 < p := by omega induction n using Nat.strong_induction_on generalizing m with | _ n ih => by_cases hm0 : m = 0 · simp [hm0, pDigitSum] have hn0 : n ≠ 0 := by omega have hrec : ∀ i, (Nat.digits p (m / p)).getD i 0 ≤ (Nat.digits p (n / p)).getD i 0 := by intro i have h3 := h (i + 1) have h4 := digits_getD_succ hp hm0 i have h5 := digits_getD_succ hp hn0 i omega have hih := ih (n / p) (Nat.div_lt_self (by omega) (by omega)) (Nat.div_le_div_right hmn) hrec have hsum_m : pDigitSum p m = m % p + pDigitSum p (m / p) := by have hd := Nat.digits_eq_cons_digits_div hp2 hm0 simp [pDigitSum, hd] have hsum_n : pDigitSum p n = n % p + pDigitSum p (n / p) := by have hd := Nat.digits_eq_cons_digits_div hp2 hn0 simp [pDigitSum, hd] have hdist : p * (n / p - m / p) = p * (n / p) - p * (m / p) := Nat.mul_sub p _ _ have h3' : m % p + p * (m / p) = m := by have h3 := Nat.mod_add_div' m p simpa [Nat.mul_comm] using h3 have h4' : n % p + p * (n / p) = n := by have h4 := Nat.mod_add_div' n p simpa [Nat.mul_comm] using h4 have hmodn : n % p < p := Nat.mod_lt (y := p) n (by omega) have halow : m % p ≤ n % p := by have h3 := h 0 have h4 := digits_getD_zero (m := m) hp have h5 := digits_getD_zero (m := n) hp omega by_cases hc : m % p = n % p · have hsub' : n - m = p * (n / p - m / p) := by omega calc pDigitSum p m + pDigitSum p (n - m) = m % p + (pDigitSum p (m / p) + pDigitSum p (n / p - m / p)) := by rw [hsum_m, hsub', pDigitSum_mul_p hp]; ring _ = m % p + pDigitSum p (n / p) := by rw [hih] _ = pDigitSum p n := by rw [hsum_n]; omega · have hsub : n - m = (n % p - m % p) + p * (n / p) - p * (m / p) := by omega have hsub' : n - m = (n % p - m % p) + p * (n / p - m / p) := by rw [hsub, Nat.add_sub_assoc (k := p * (m / p)) (m := p * (n / p)) (Nat.mul_le_mul_left p (Nat.div_le_div_right hmn)), hdist] have hlowpos : 0 < n % p - m % p := (Nat.sub_pos_iff_lt).2 (by omega) have hd := Nat.digits_add p hp2 (n % p - m % p) (n / p - m / p) (Nat.lt_of_le_of_lt (Nat.sub_le _ _) hmodn) (Or.inl (by omega)) have hsum : pDigitSum p ((n % p - m % p) + p * (n / p - m / p)) = (n % p - m % p) + pDigitSum p (n / p - m / p) := by simp [pDigitSum, hd] calc pDigitSum p m + pDigitSum p (n - m) = m % p + (pDigitSum p (m / p) + ((n % p - m % p) + pDigitSum p (n / p - m / p))) := by rw [hsum_m, hsub', hsum]; ring _ = n % p + pDigitSum p (n / p) := by omega _ = pDigitSum p n := by rw [hsum_n] /-- If every base-`p` digit of `m` is at most the corresponding digit of `n`, then `p` does not divide `Nat.choose n m`. -/ theorem not_dvd_choose_of_digits_le {p m n : ℕ} (hp : p.Prime) (hmn : m ≤ n) (h : ∀ i, (Nat.digits p m).getD i 0 ≤ (Nat.digits p n).getD i 0) : ¬ p ∣ Nat.choose n m := by intro hd obtain ⟨i, hi⟩ := (dvd_choose_iff_digits_gt hp hmn).mp hd exact Nat.not_le_of_gt hi (h i) /-- Two digit lists of the same length, the first dominating the second digitwise, represent numbers in nondecreasing order. -/ theorem ofDigits_le_of_getD_le {b : ℕ} {l1 l2 : List ℕ} (hlen : l1.length = l2.length) (hle : ∀ i, l1.getD i 0 ≤ l2.getD i 0) : Nat.ofDigits b l1 ≤ Nat.ofDigits b l2 := by induction l1 generalizing l2 with | nil => have hl2 : l2 = [] := List.eq_nil_of_length_eq_zero hlen.symm simp [hl2] | cons a t ih => cases l2 with | nil => simp at hlen | cons b' t' => have hlen' : t.length = t'.length := by simp at hlen; omega have h0 : a ≤ b' := by simpa only [List.getD_cons_zero] using hle 0 have htail : ∀ i, t.getD i 0 ≤ t'.getD i 0 := by intro i simpa only [List.getD_cons_succ] using hle (i + 1) have hle' : Nat.ofDigits b t ≤ Nat.ofDigits b t' := ih (l2 := t') hlen' htail have hmul : b * Nat.ofDigits b t ≤ b * Nat.ofDigits b t' := Nat.mul_le_mul_left b hle' simp only [Nat.ofDigits_cons] omega /-- Two different digit lists of the same length, the first dominating the second digitwise, represent numbers in strictly increasing order. -/ theorem ofDigits_lt_of_getD_le {b : ℕ} (hb : 1 < b) {l1 l2 : List ℕ} (hlen : l1.length = l2.length) (hle : ∀ i, l1.getD i 0 ≤ l2.getD i 0) (hne : l1 ≠ l2) : Nat.ofDigits b l1 < Nat.ofDigits b l2 := by induction l1 generalizing l2 with | nil => exact (hne (List.eq_nil_of_length_eq_zero hlen.symm).symm).elim | cons a t ih => cases l2 with | nil => simp at hlen | cons b' t' => have hlen' : t.length = t'.length := by simp at hlen; omega have h0 : a ≤ b' := by simpa only [List.getD_cons_zero] using hle 0 have htail : ∀ i, t.getD i 0 ≤ t'.getD i 0 := by intro i simpa only [List.getD_cons_succ] using hle (i + 1) simp only [Nat.ofDigits_cons] by_cases hab : a = b' · rw [hab] have hlt' : Nat.ofDigits b t < Nat.ofDigits b t' := by apply ih (l2 := t') hlen' htail intro he apply hne simp only [List.cons.injEq] exact ⟨hab, he⟩ have hmul : b * Nat.ofDigits b t < b * Nat.ofDigits b t' := (Nat.mul_lt_mul_left (a := b) (by omega)).mpr hlt' omega · have hle' : Nat.ofDigits b t ≤ Nat.ofDigits b t' := ofDigits_le_of_getD_le hlen' htail have hmul : b * Nat.ofDigits b t ≤ b * Nat.ofDigits b t' := Nat.mul_le_mul_left b hle' omega /-- A nonempty digit list whose last digit is nonzero represents a positive number. -/ theorem ofDigits_pos_of_getLast_ne_zero {b : ℕ} (hb : 1 < b) : ∀ (l : List ℕ) (hl : l ≠ []) (hlast : l.getLast hl ≠ 0), 0 < Nat.ofDigits b l := by intro l induction l with | nil => intro hl _; exact (hl rfl).elim | cons a t ih => intro hl hlast rcases a with _ | a · have ht : t ≠ [] := by intro hte simp [hte] at hlast have hgetLast : t.getLast ht ≠ 0 := by rw [List.getLast_cons ht] at hlast exact hlast have hpos := ih ht hgetLast simpa only [Nat.ofDigits_cons, zero_add] using Nat.mul_pos (by omega) hpos · simp only [Nat.ofDigits_cons] omega /-- The digit sum of a nonzero number is positive. -/ theorem pDigitSum_pos {p x : ℕ} (hp : 2 ≤ p) (hx : 0 < x) : 0 < pDigitSum p x := by have hne : x ≠ 0 := Nat.ne_of_gt hx have hne0 : Nat.digits p x ≠ [] := Nat.digits_ne_nil_iff_ne_zero.mpr hne have hlast0 : (Nat.digits p x).getLast hne0 ≠ 0 := Nat.getLast_digit_ne_zero p hne rw [pDigitSum, List.sum_pos_iff_exists_pos_nat] exact ⟨(Nat.digits p x).getLast hne0, List.getLast_mem hne0, Nat.pos_of_ne_zero hlast0⟩ /-- **Digit reduction.** If `L` is a base-`p` digit list (all entries `< p`, last entry nonzero) whose digit sum is `> q > 0`, then there is a digit list `E` of the same length whose digit sum is exactly `q`, whose entries are digitwise at most those of `L`, and which is again a base-`p` digit list. The reduction takes the required `q` units greedily, so that the leading digit stays nonzero. -/ theorem exists_list_getD_le_sum_eq {p q : ℕ} (hp2 : 2 ≤ p) (hq : 0 < q) : ∀ (L : List ℕ) (hL : L ≠ []), (∀ d ∈ L, d < p) → L.getLast hL ≠ 0 → q < L.sum → ∃ (E : List ℕ) (hE : E ≠ []), E.length = L.length ∧ E.sum = q ∧ (∀ d ∈ E, d < p) ∧ E.getLast hE ≠ 0 ∧ (∀ i, E.getD i 0 ≤ L.getD i 0) := by intro L induction L with | nil => intro _ _ _ hsum; simp at hsum | cons a t ih => intro _ hL hlast hsum have hsum' : q < a + t.sum := by simpa using hsum have hat : a < p := hL a List.mem_cons_self have htall : ∀ d ∈ t, d < p := fun d hd => hL d (List.mem_cons_of_mem a hd) by_cases ht : q < t.sum · have ht0 : t ≠ [] := by intro hc have h0 : t.sum = 0 := by rw [hc]; rfl omega have hlast_t : t.getLast ht0 ≠ 0 := by rw [List.getLast_cons ht0] at hlast exact hlast obtain ⟨E, hE0, hElen, hEsum, hEall, hElast, hEdom⟩ := ih ht0 htall hlast_t ht refine ⟨0 :: E, by simp, by simp [hElen], by simpa using hEsum, ?_, ?_, ?_⟩ · intro d hd rcases List.mem_cons.mp hd with rfl | hd · omega · exact hEall d hd · rw [List.getLast_cons hE0] exact hElast · intro i cases i with | zero => simp | succ i => simpa only [List.getD_cons_succ] using hEdom i · by_cases ht0 : t = [] · have htsum0 : t.sum = 0 := by rw [ht0]; rfl have haq : q < a := by omega refine ⟨q :: [], by simp, by simp [ht0], by simp, ?_, ?_, ?_⟩ · intro d hd rcases List.mem_cons.mp hd with rfl | hd · omega · simp at hd · have hgl : (q :: []).getLast (by simp : q :: [] ≠ []) = q := by simp rw [hgl] omega · intro i cases i with | zero => simp only [List.getD_cons_zero] omega | succ i => simp only [List.getD_cons_succ, List.getD_nil] exact Nat.zero_le _ · have htsum_le : t.sum ≤ q := by omega have htlast : t.getLast ht0 ≠ 0 := by rw [List.getLast_cons ht0] at hlast exact hlast refine ⟨(q - t.sum) :: t, by simp, by simp, Nat.sub_add_cancel htsum_le, ?_, ?_, ?_⟩ · intro d hd rcases List.mem_cons.mp hd with rfl | hd · omega · exact htall d hd · rw [List.getLast_cons ht0] exact htlast · intro i cases i with | zero => simp only [List.getD_cons_zero] omega | succ i => simp only [List.getD_cons_succ] exact Nat.le_refl _ /-- **Theorem Q.** For `q < n` and a prime `p ≡ 1 mod q`, the exponent of `p` in the gcd of the binomial coefficients `C(n, q k)` over `0 < k < n / q` is `1` when the base-`p` digit sum of `n` is at most `q`, and `0` otherwise. -/ theorem binomial_gcd_valuation (n q : ℕ) (hq : 0 < q) (hn : q < n) (p : ℕ) (hp : p.Prime) (hcon : p % q = 1) : Nat.factorization (binomialGcd n q) p = if pDigitSum p n ≤ q then 1 else 0 := by have hp2 : 2 ≤ p := hp.two_le have hp1 : 1 < p := by omega have hn0 : 0 < n := by omega have hqdiv : q ∣ p - 1 := by have h := mod_add_div_one hcon rw [h] simpa using Nat.dvd_mul_right (p / q) q have hgpos : 0 < binomialGcd n q := binomialGcd_pos n q hq hn have hdvg : p ∣ binomialGcd n q ↔ 1 ≤ Nat.factorization (binomialGcd n q) p := Nat.Prime.dvd_iff_one_le_factorization hp (Nat.ne_of_gt hgpos) by_cases hs : pDigitSum p n ≤ q · rw [if_pos hs] have hpnn : p ≤ n := by by_contra hcon' have hnlt : n < p := by omega have hd : Nat.digits p n = [n] := by rw [Nat.digits_eq_cons_digits_div hp1 hn0.ne', Nat.div_eq_of_lt hnlt, Nat.mod_eq_of_lt hnlt, Nat.digits_zero] have hsum : pDigitSum p n = n := by simp [pDigitSum, hd] omega have hstep : ∀ m : ℕ, q ∣ m → 0 < m → m < n → p ∣ Nat.choose n m := by intro m hqm hm0 hmn by_contra hnond have hle : ∀ i, (Nat.digits p m).getD i 0 ≤ (Nat.digits p n).getD i 0 := by intro i by_contra hc exact hnond ((dvd_choose_iff_digits_gt hp (Nat.le_of_lt hmn)).2 ⟨i, Nat.lt_of_not_ge hc⟩) have hsumadd := digit_sum_add_sub_of_digits_le hp2 (Nat.le_of_lt hmn) hle have hdvd : q ∣ pDigitSum p m := by have h := (Nat.modEq_digits_sum q p hcon m).symm.trans (Nat.modEq_zero_iff_dvd.mpr hqm) exact Nat.modEq_zero_iff_dvd.mp h have hlow : q ≤ pDigitSum p m := Nat.le_of_dvd (pDigitSum_pos hp2 hm0) hdvd have hpos2 : 0 < pDigitSum p (n - m) := pDigitSum_pos hp2 (by omega) omega have hstepall : p ∣ binomialGcd n q := by refine Finset.dvd_gcd (fun k hk => ?_) have hk' : k < n ∧ 0 < k ∧ q * k < n := by simpa only [binomialIndices, Finset.mem_filter, Finset.mem_range] using hk exact hstep (q * k) (dvd_mul_of_dvd_left (Nat.dvd_refl _) _) (Nat.mul_pos hq hk'.2.1) hk'.2.2 set b := (Nat.digits p n).length have hb2 : 2 ≤ b := by have h := (Nat.lt_digits_length_iff (b := p) (k := 1) hp1 n).2 (by simpa using hpnn) omega have hnb : n < p ^ b := (Nat.digits_length_le_iff (b := p) (k := b) hp1 n).mp (le_refl b) have hlog : Nat.log p n < b := Nat.log_lt_of_lt_pow hn0.ne' hnb set m := (p - 1) * p ^ (b - 2) have hqm : q ∣ m := dvd_trans hqdiv (by show p - 1 ∣ (p - 1) * p ^ (b - 2) simpa [Nat.mul_comm] using Nat.dvd_mul_left (p - 1) (p ^ (b - 2))) have hm0 : 0 < m := by have h1 : 1 ≤ p - 1 := by omega have h2 : 1 ≤ p ^ (b - 2) := Nat.one_le_pow (b - 2) p (by omega) have h3 : 1 ≤ (p - 1) * p ^ (b - 2) := Nat.mul_le_mul h1 h2 omega have hmnn : m < n := by have hlt : (p - 1) * p ^ (b - 2) < p * p ^ (b - 2) := (Nat.mul_lt_mul_right (Nat.pow_pos (a := p) (n := b - 2) (by omega))).mpr (by omega) have hpow : p * p ^ (b - 2) = p ^ (b - 1) := by have h1 : b - 1 = 1 + (b - 2) := by omega rw [h1, Nat.pow_add, pow_one] have hle : p ^ (b - 1) ≤ n := (Nat.lt_digits_length_iff (b := p) (k := b - 1) hp1 n).mp (by omega) omega have hdivm : ∀ i, i ≤ b - 2 → p ^ i ∣ m := by intro i hi refine dvd_trans (Nat.pow_dvd_pow p hi) (by show p ^ (b - 2) ∣ (p - 1) * p ^ (b - 2) exact Nat.dvd_mul_left (p ^ (b - 2)) (p - 1)) have hcardle : ((Finset.Ico 1 b).filter (fun i => p ^ i ≤ m % p ^ i + (n - m) % p ^ i)).card ≤ 1 := by rw [Finset.card_le_one] intro i hi j hj simp only [Finset.mem_filter, Finset.mem_Ico] at hi hj by_cases h1 : i ≤ b - 2 · have hmod0 : m % p ^ i = 0 := Nat.mod_eq_zero_of_dvd (hdivm i h1) have hpos : 0 < p ^ i := Nat.pow_pos (a := p) (n := i) (by omega) have hmodlt : (n - m) % p ^ i < p ^ i := Nat.mod_lt _ hpos exfalso rw [hmod0] at hi omega · have hib : i = b - 1 := by omega have hjb : j = b - 1 := by by_contra hc have h1' : j ≤ b - 2 := by omega have hmod0 : m % p ^ j = 0 := Nat.mod_eq_zero_of_dvd (hdivm j h1') have hpos : 0 < p ^ j := Nat.pow_pos (a := p) (n := j) (by omega) have hmodlt : (n - m) % p ^ j < p ^ j := Nat.mod_lt _ hpos rw [hmod0] at hj omega omega have hup : Nat.factorization (Nat.choose n m) p ≤ 1 := by rw [Nat.factorization_choose hp (Nat.le_of_lt hmnn) hlog] exact hcardle have hqkm : q * (m / q) = m := by rw [Nat.mul_comm] exact Nat.div_mul_cancel hqm have hkmem : m / q ∈ binomialIndices n q := by have hlt : q * (m / q) < n := by omega have hdivpos : 0 < m / q := by by_contra hc have hc0 : m / q = 0 := Nat.eq_zero_of_not_pos hc rw [← hqkm, hc0] at hm0 simp at hm0 have hdivlt : m / q < n := by have h1 : m / q ≤ q * (m / q) := by have h2 := Nat.mul_le_mul_left (m / q) (by omega : 1 ≤ q) simpa [Nat.mul_comm] using h2 omega simp only [binomialIndices, Finset.mem_filter, Finset.mem_range] exact ⟨hdivlt, hdivpos, hlt⟩ have hdv : binomialGcd n q ∣ Nat.choose n m := by rw [← hqkm] exact binomialGcd_dvd_all n q (m / q) hkmem have hfac_le : Nat.factorization (binomialGcd n q) p ≤ Nat.factorization (Nat.choose n m) p := Nat.factorization_le_factorization_of_dvd_right hdv (Nat.ne_of_gt hgpos) (Nat.choose_ne_zero (Nat.le_of_lt hmnn)) have hlow : 1 ≤ Nat.factorization (binomialGcd n q) p := hdvg.mp hstepall omega · rw [if_neg hs] have hsum : q < (Nat.digits p n).sum := Nat.lt_of_not_ge hs obtain ⟨E, hE, hElen, hEsum, hEall, hElast, hEdom⟩ := exists_list_getD_le_sum_eq hp2 hq (Nat.digits p n) (Nat.digits_ne_nil_iff_ne_zero.mpr hn0.ne') (fun d hd => Nat.digits_lt_base hp1 hd) (Nat.getLast_digit_ne_zero p hn0.ne') hsum set m := Nat.ofDigits p E have hmdig : Nat.digits p m = E := Nat.digits_ofDigits p hp1 E hEall (fun _ => hElast) have hm0 : 0 < m := ofDigits_pos_of_getLast_ne_zero hp1 E hE hElast have hEne : E ≠ Nat.digits p n := by intro he have h : (Nat.digits p n).sum = q := by rw [he] at hEsum; exact hEsum omega have hmlt : m < n := by have h1 := ofDigits_lt_of_getD_le (b := p) hp1 hElen hEdom hEne rw [Nat.ofDigits_digits] at h1 exact h1 have hqm : q ∣ m := by have h : m ≡ 0 [MOD q] := by refine (Nat.modEq_digits_sum q p hcon m).trans ?_ rw [hmdig, hEsum] exact Nat.modEq_zero_iff_dvd.mpr (Nat.dvd_refl q) exact Nat.modEq_zero_iff_dvd.mp h have hqkm : q * (m / q) = m := by rw [Nat.mul_comm] exact Nat.div_mul_cancel hqm have hkmem : m / q ∈ binomialIndices n q := by have hlt : q * (m / q) < n := by omega have hdivpos : 0 < m / q := by by_contra hc have hc0 : m / q = 0 := Nat.eq_zero_of_not_pos hc rw [← hqkm, hc0] at hm0 simp at hm0 have hdivlt : m / q < n := by have h1 : m / q ≤ q * (m / q) := by have h2 := Nat.mul_le_mul_left (m / q) (by omega : 1 ≤ q) simpa [Nat.mul_comm] using h2 omega simp only [binomialIndices, Finset.mem_filter, Finset.mem_range] exact ⟨hdivlt, hdivpos, hlt⟩ have hdv : binomialGcd n q ∣ Nat.choose n m := by rw [← hqkm] exact binomialGcd_dvd_all n q (m / q) hkmem have hnond : ¬ p ∣ Nat.choose n m := by intro hpc have hnot := not_dvd_choose_of_digits_le hp (m := q * (m / q)) (n := n) (by omega) (fun i => by rw [hqkm, hmdig]; exact hEdom i) rw [← hqkm] at hpc exact hnot hpc have hnondg : ¬ p ∣ binomialGcd n q := fun h => hnond (dvd_trans h hdv) have hz := hdvg.not.mp hnondg exact Nat.le_zero.mp (by omega)