| BLOG |
Carl McTague
mathematician, hacker, composer, fiddler |
Autoformalization on a PC
“He does little himself. He only plans. But his agents are numerous and splendidly organized.”
—Sherlock Holmes on Professor Moriarty in “The Final Problem” (1893)
I dropped the English-language LaTeX source file of my Monthly paper On the Greatest Common Divisor of Binomial Coefficients (arXiv:1510.06696) into an OpenCode session running the open-weight model Qwen 3.8-27b locally on a Framework Desktop PC.
The agent used just 19GB of VRAM with a 256k-token context window (which OpenCode repeatedly recompacted as it went along). I connected it to a locally running Lean 4 process over the Model Context Protocol (MCP) so that it could inspect Lean’s proof state in real time rather than repeatedly running batch builds.
In a few hours, the agent formalized the paper’s main theorem completely on its own:
TheoremQwen.lean— autonomous formalization by Qwen 3.8-27b.
The only steps which didn’t take place entirely offline were remote queries made by the agent to Loogle to search Mathlib for applicable lemmas (though Loogle can be indexed and run locally as well).
I autoformalized the theorem a second time using the open-weight model DeepSeek V4.1 Flash 1017, again running locally on the same desktop hardware.
This model has 748 billion parameters (1.5TB of uncompressed weights), quantized by the community down to 341GB on disk. While the Qwen run fit neatly into 19GB of VRAM, this second model requires significantly more system RAM (up to 128GB). However, using DwarfStar (ds4), the system streams individual sparse expert weights into system RAM on the fly as needed via memory-mapped I/O (mmap). This makes it possible to execute massive Mixture of Experts (MoE) models on consumer desktop hardware without a datacenter cluster.
TheoremQ.lean— autonomous formalization by DeepSeek V4.1 Flash 1017.
Here is DeepSeek’s formalization of the paper’s main theorem:
/--
McTague's Theorem Q:
For integers n > q > 0 and a prime p ≡ 1 [MOD q], the p-adic valuation
of gcd_{0 < k < n/q} {n \choose q*k} is 1 if the sum of base-p digits
of n is ≤ q, and 0 otherwise.
-/
theorem theoremQ {n q p : ℕ} (hq : 0 < q) (hnq : q < n) (hp : Nat.Prime p)
(hpq : p ≡ 1 [MOD q]) :
padicValNat p (gcd_of_choose n q) = if alpha p n ≤ q then 1 else 0 := by
split_ifs with halpha
· exact theoremQ_if hq hnq hp hpq halpha
· exact theoremQ_onlyIf hq hp hpq (by omega)
Click to expand DeepSeek’s formalization of the proof (~600 lines)
/-
Copyright (c) 2026 Carl McTague. All rights reserved.
Released under Apache 2.0 license.
Authors: Carl McTague, autoformalized from .tex file by DeepSeek v4.1 Flash 1017 on a PC (Framework Desktop)
-/
import Mathlib.Data.Nat.Choose.Basic
import Mathlib.Data.Nat.Digits.Lemmas
import Mathlib.Data.Nat.Digits.Div
import Mathlib.Data.Nat.ModEq
import Mathlib.Data.Nat.Prime.Basic
import Mathlib.Data.Nat.Find
import Mathlib.Data.Finset.Card
import Mathlib.Data.Finset.Insert
import Mathlib.NumberTheory.Padics.PadicVal.Basic
import Mathlib.Algebra.GCDMonoid.Finset
import Mathlib.Algebra.Order.BigOperators.Group.List
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Ring
/-!
# On the Greatest Common Divisor of Binomial Coefficients
For integers `n > q > 0` and a prime `p` congruent to `1` modulo `q`,
the `p`-adic valuation of the gcd of the binomial coefficients
`n.choose (q*k)` for `0 < k < n/q` is `1` when the sum of the base-`p`
digits of `n` is at most `q`, and `0` otherwise.
## References
* [C. McTague, *On the Greatest Common Divisor of Binomial Coefficients*](https://doi.org/10.4169/amer.math.monthly.124.4.353)
-/
/-- The sum of the base-`p` digits of `n` (McTague's `alpha_p(n)`). -/
def alpha (p n : ℕ) : ℕ :=
(Nat.digits p n).sum
/-- The set of `k` with `0 < k < n/q`, encoded as `0 < k` and `q*k < n`. -/
def choose_k_set (n q : ℕ) : Finset ℕ :=
(Finset.range n).filter (fun k => 0 < k ∧ q * k < n)
/-- The gcd of the binomial coefficients `n.choose (q*k)` for `0 < k < n/q`. -/
def gcd_of_choose (n q : ℕ) : ℕ :=
(choose_k_set n q).gcd (fun k => n.choose (q * k))
/-- No carries occur when adding `a` to `b - a` in base `p`, expressed by
comparing the lower `i`-digit blocks of `a` and `b`. -/
def NoCarry (p a b : ℕ) : Prop :=
∀ i : ℕ, a % p ^ i ≤ b % p ^ i
lemma le_of_noCarry {p a b : ℕ} (hp : 1 < p) (h : NoCarry p a b) : a ≤ b := by
by_cases hab : a < b
· exact le_of_lt hab
· have hba : b ≤ a := by omega
have hpa : a < p ^ (a + 1) := by
have h1 : a < p ^ a := Nat.lt_pow_self hp
have h2 : p ^ a < p ^ (a + 1) := Nat.pow_lt_pow_right hp (by omega)
omega
have hpb : b < p ^ (a + 1) := by omega
have hh := h (a + 1)
rw [Nat.mod_eq_of_lt hpa, Nat.mod_eq_of_lt hpb] at hh
exact hh
lemma mem_choose_k_set {n q k : ℕ} (hq : 0 < q) :
k ∈ choose_k_set n q ↔ 0 < k ∧ q * k < n := by
simp only [choose_k_set, Finset.mem_filter, Finset.mem_range]
constructor
· intro h
exact ⟨h.2.1, h.2.2⟩
· intro h
have hle : k ≤ q * k := Nat.le_mul_of_pos_left k hq
exact ⟨by omega, h⟩
lemma gcd_of_choose_ne_zero {n q : ℕ} (hq : 0 < q) (hnq : q < n) :
gcd_of_choose n q ≠ 0 := by
have hk : 1 ∈ choose_k_set n q := by rw [mem_choose_k_set hq]; omega
have hkn : q * 1 < n := ((mem_choose_k_set hq).mp hk).2
rw [gcd_of_choose, Finset.gcd_ne_zero_iff]
exact ⟨1, hk, Nat.choose_ne_zero hkn.le⟩
lemma q_dvd_p_sub_one_of_prime_modEq {p q : ℕ} (hq : 0 < q)
(hpq : p ≡ 1 [MOD q]) :
q ∣ p - 1 := by
by_cases hq1 : q = 1
· subst q
exact one_dvd _
· have hq1' : 1 < q := by omega
have hpq' : p % q = 1 := by
have h : p % q = 1 % q := hpq
simpa [Nat.mod_eq_of_lt hq1'] using h
simpa [hpq'] using Nat.dvd_sub_mod (n := q) p
lemma q_lt_p_of_prime_modEq {p q : ℕ} (hp : Nat.Prime p) (hq : 0 < q)
(hpq : p ≡ 1 [MOD q]) :
q < p := by
have hdvd : q ∣ p - 1 := q_dvd_p_sub_one_of_prime_modEq hq hpq
have hp1 : 0 < p - 1 := by
have hp2 : 2 ≤ p := hp.two_le
omega
have hle : q ≤ p - 1 := Nat.le_of_dvd hp1 hdvd
omega
lemma carry_iff_mod_gt {p i k n : ℕ} (hk : k ≤ n) (hp : 0 < p) :
p ^ i ≤ k % p ^ i + (n - k) % p ^ i ↔ n % p ^ i < k % p ^ i := by
set m := p ^ i
have hmpos : 0 < m := Nat.pow_pos hp
have hkn : k + (n - k) = n := Nat.add_sub_cancel' hk
have hmod : (k % m + (n - k) % m) % m = n % m := by
rw [← Nat.add_mod k (n - k) m, hkn]
have hite := Nat.add_mod_eq_ite (k := m) (m := k % m) (n := (n - k) % m)
simp only [Nat.mod_mod] at hite
rw [hmod] at hite
have hdlt : (n - k) % m < m := Nat.mod_lt _ hmpos
by_cases hc : m ≤ k % m + (n - k) % m
· rw [ite_eq_left hc] at hite
constructor <;> omega
· rw [ite_eq_right hc] at hite
constructor <;> omega
lemma padicValNat_choose_eq_zero_iff_noCarry {p n k : ℕ} (hp : Nat.Prime p)
(hk : k ≤ n) :
padicValNat p (n.choose k) = 0 ↔ NoCarry p k n := by
have : Fact (Nat.Prime p) := ⟨hp⟩
let b := Nat.log p n + 1
rw [padicValNat_choose (p := p) (n := n) (k := k) (b := b) hk (by omega)]
rw [Finset.card_filter_eq_zero_iff]
constructor
· intro h i
by_cases hi0 : i = 0
· subst i
simp only [Nat.pow_zero, Nat.mod_one]
exact Nat.zero_le _
· by_cases hib : i < b
· have hmem : i ∈ Finset.Ico 1 b := Finset.mem_Ico.mpr ⟨by omega, hib⟩
have hncarry := h i hmem
have hiff := carry_iff_mod_gt (p := p) (i := i) (k := k) (n := n) hk hp.pos
rw [hiff] at hncarry
omega
· have hpn : n < p ^ i := by
have hb : n < p ^ b := by
simpa [b] using Nat.lt_pow_succ_log_self hp.one_lt n
have hb_le : p ^ b ≤ p ^ i := Nat.pow_le_pow_right hp.pos (by omega)
omega
have hkp : k < p ^ i := by omega
rw [Nat.mod_eq_of_lt hkp, Nat.mod_eq_of_lt hpn]
omega
· intro h i hi
have hiff := carry_iff_mod_gt (p := p) (i := i) (k := k) (n := n) hk hp.pos
have hle := h i
rw [hiff]
omega
lemma alpha_modEq {p q m : ℕ} (hq : 0 < q) (hpq : p ≡ 1 [MOD q]) :
alpha p m ≡ m [MOD q] := by
by_cases hq1 : q = 1
· subst q
exact Nat.modEq_one
· have hq1' : 1 < q := by omega
have hpq' : p % q = 1 := by
have h : p % q = 1 % q := hpq
simpa [Nat.mod_eq_of_lt hq1'] using h
exact (Nat.modEq_digits_sum q p hpq' m).symm
lemma ofDigits_eq_zero_of_forall_zero (p : ℕ) {L : List ℕ}
(h : ∀ x ∈ L, x = 0) : Nat.ofDigits p L = 0 := by
rw [Nat.ofDigits_eq_sum_mapIdx, List.sum_eq_zero_iff_forall_eq_nat]
intro x hx
rcases List.mem_mapIdx.mp hx with ⟨i, hi, rfl⟩
simp [h (L[i]) (List.getElem_mem hi)]
lemma alpha_eq_zero_iff {p n : ℕ} : alpha p n = 0 ↔ n = 0 := by
constructor
· intro h
have hall : ∀ x ∈ Nat.digits p n, x = 0 := List.sum_eq_zero_iff_forall_eq_nat.mp h
have hof : Nat.ofDigits p (Nat.digits p n) = 0 := ofDigits_eq_zero_of_forall_zero p hall
rwa [Nat.ofDigits_digits] at hof
· intro h
subst n
simp [alpha, Nat.digits_zero]
lemma alpha_pos_of_pos {p n : ℕ} (hn : 0 < n) :
0 < alpha p n := by
have hne : alpha p n ≠ 0 := by
intro h
exact hn.ne' (alpha_eq_zero_iff.mp h)
exact Nat.pos_of_ne_zero hne
lemma alpha_lt_of_choose_eq_zero {p n k : ℕ} (hp : Nat.Prime p) (hk : k < n)
(h : padicValNat p (n.choose k) = 0) :
alpha p k < alpha p n := by
have : Fact (Nat.Prime p) := ⟨hp⟩
have hsum :=
sub_one_mul_padicValNat_choose_eq_sub_sum_digits (p := p) (k := k) (n := n)
(by omega : k ≤ n)
rw [h] at hsum
simp only [Nat.mul_zero] at hsum
have hle : alpha p k + alpha p (n - k) ≤ alpha p n := by
exact Nat.sub_eq_zero_iff_le.mp hsum.symm
have hBpos : 0 < alpha p (n - k) := alpha_pos_of_pos (by omega)
omega
lemma digit_eq_getElem {p n i : ℕ} (hp : Nat.Prime p)
(hi : i < (Nat.digits p n).length) :
n / p ^ i % p = (Nat.digits p n)[i] := by
rw [List.getElem_eq_getD (l := Nat.digits p n) (i := i) (fallback := 0)]
exact (Nat.getD_digits n i hp.two_le).symm
lemma take_sum_le_alpha {p n j : ℕ} :
((Nat.digits p n).take j).sum ≤ alpha p n := by
have hsub :=
List.Sublist.sum_le_sum (List.take_sublist j (Nat.digits p n))
(by intro a ha; exact Nat.zero_le a)
simpa [alpha] using hsub
lemma digits_pair_le_alpha {p n i : ℕ} (hi0 : 0 < i)
(hi : i < (Nat.digits p n).length) :
(Nat.digits p n)[i] + (Nat.digits p n)[i - 1] ≤ alpha p n := by
let L := Nat.digits p n
let d := L[i]
let l := L[i - 1]
have hi' : i < L.length := hi
have hlt : i - 1 < (List.take i L).length := by
rw [List.length_take]
omega
have hget : (List.take i L)[i - 1] = L[i - 1] :=
List.getElem_take (xs := L) (j := i) (i := i - 1) (h := hlt)
have htake_i : (List.take i L).sum ≤ alpha p n := by
simpa [L] using take_sum_le_alpha (p := p) (n := n) (j := i)
have hmem : l ∈ List.take i L := by
change L[i - 1] ∈ List.take i L
rw [← hget]
exact List.getElem_mem hlt
have hl_le : l ≤ (List.take i L).sum := List.le_sum_of_mem hmem
have htake_succ : (List.take (i + 1) L).sum = (List.take i L).sum + d := by
simpa [d] using List.sum_take_succ L i hi'
have htake_succ_le : (List.take (i + 1) L).sum ≤ alpha p n := by
simpa [L] using take_sum_le_alpha (p := p) (n := n) (j := i + 1)
change d + l ≤ alpha p n
omega
lemma top_digit_pos {p n i : ℕ} (hp : Nat.Prime p) (hn : 0 < n)
(hi_log : i = Nat.log p n) (hi : i < (Nat.digits p n).length) :
0 < (Nat.digits p n)[i] := by
have hlt : n / p ^ i < p := by
have h1 : n < p ^ (i + 1) := by
simpa [hi_log] using Nat.lt_pow_succ_log_self hp.one_lt n
have h2 : p ^ (i + 1) = p ^ i * p := by rw [Nat.pow_succ]
exact Nat.div_lt_of_lt_mul (by simpa [h2, mul_comm] using h1)
have hmod := digit_eq_getElem (p := p) (n := n) (i := i) hp hi
rw [Nat.mod_eq_of_lt hlt] at hmod
rw [← hmod]
exact Nat.div_pos (by simpa [hi_log] using Nat.pow_log_le_self p hn.ne') (Nat.pow_pos hp.pos)
lemma top_pred_digit_lt {p n q i : ℕ} (hp : Nat.Prime p) (hq : 0 < q)
(hpq : p ≡ 1 [MOD q]) (hnq : q < n) (halpha : alpha p n ≤ q)
(hi_log : i = Nat.log p n) (hi_pos : 0 < i)
(hi : i < (Nat.digits p n).length) :
n / p ^ (i - 1) % p < p - 1 := by
let L := Nat.digits p n
let l := L[i - 1]
let d := L[i]
have hi' : i < L.length := hi
have hi1 : i - 1 < L.length := by omega
have hdigit_l : n / p ^ (i - 1) % p = l := by
simpa [L, l] using digit_eq_getElem (p := p) (n := n) (i := i - 1) hp hi1
have hd_pos : 0 < d := by
simpa [L, d] using top_digit_pos (p := p) (n := n) (i := i) hp (by omega) hi_log hi
have hpair : d + l ≤ alpha p n := by
simpa [L, d, l] using digits_pair_le_alpha (p := p) (n := n) (i := i) hi_pos hi
have hlt_p : l < p := by
exact Nat.digits_lt_base hp.one_lt (List.getElem_mem hi1)
have hqp : q < p := q_lt_p_of_prime_modEq hp hq hpq
have hne : l ≠ p - 1 := by
intro heq
omega
omega
lemma top_exp_pos_of_alpha_le {p n q : ℕ} (hp : Nat.Prime p) (hnq : q < n)
(halpha : alpha p n ≤ q) :
0 < Nat.log p n := by
by_contra hpos
have hi0 : Nat.log p n = 0 := by omega
rcases (Nat.log_eq_zero_iff.mp hi0) with hlt | hle
· have hdig : Nat.digits p n = [n] := Nat.digits_of_lt p n (by omega) hlt
have halpha_eq : alpha p n = n := by simp [alpha, hdig]
omega
· have hp2 : 2 ≤ p := hp.two_le
omega
lemma pred_mul_pow_lt_pow {p i : ℕ} (hp : 1 < p) (hi : 0 < i) :
(p - 1) * p ^ (i - 1) < p ^ i := by
have hm1 : i - 1 + 1 = i := Nat.sub_add_cancel hi
have hpow : p ^ i = p * p ^ (i - 1) := by
simpa [hm1] using (Nat.pow_succ' (m := p) (n := i - 1))
have hpos : 0 < p ^ (i - 1) := Nat.pow_pos (by omega)
have hlt : p - 1 < p := by omega
have hmul := Nat.mul_lt_mul_of_pos_right hlt hpos
simpa [hpow] using hmul
lemma kummer_filter_eq_singleton {p n i : ℕ} (hp : Nat.Prime p)
(hi_pos : 0 < i)
(hl : n / p ^ (i - 1) % p < p - 1)
(hKlt : (p - 1) * p ^ (i - 1) < n) :
let K := (p - 1) * p ^ (i - 1)
{x ∈ Finset.Ico 1 (i + 1) | p ^ x ≤ K % p ^ x + (n - K) % p ^ x} = {i} := by
let K := (p - 1) * p ^ (i - 1)
have hKle : K ≤ n := by omega
have hm1 : i - 1 + 1 = i := Nat.sub_add_cancel hi_pos
have hKlt_pi : K < p ^ i := by
simpa [K] using pred_mul_pow_lt_pow hp.one_lt hi_pos
ext x
constructor
· intro hx
have hxcarry : p ^ x ≤ K % p ^ x + (n - K) % p ^ x := (Finset.mem_filter.mp hx).2
have hxlt : x < i + 1 := (Finset.mem_Ico.mp (Finset.mem_filter.mp hx).1).2
have hiff := carry_iff_mod_gt (p := p) (i := x) (k := K) (n := n) hKle hp.pos
have hgt : n % p ^ x < K % p ^ x := hiff.mp hxcarry
by_cases hxi : x < i
· have hdvd0 : p ^ x ∣ p ^ (i - 1) := Nat.pow_dvd_pow p (by omega)
have hdvd : p ^ x ∣ K := by
have := dvd_mul_of_dvd_right hdvd0 (p - 1)
simpa [K, mul_comm] using this
have hKmod : K % p ^ x = 0 := Nat.mod_eq_zero_of_dvd hdvd
rw [hKmod] at hgt
exact absurd hgt (Nat.not_lt_zero _)
· have hxeq : x = i := by omega
rw [hxeq]
simp
· intro hx
have hxi : x = i := by simpa using hx
subst x
have hi_mem : i ∈ Finset.Ico 1 (i + 1) := by
rw [Finset.mem_Ico]
omega
have hcarry : p ^ i ≤ K % p ^ i + (n - K) % p ^ i := by
have hiff := carry_iff_mod_gt (p := p) (i := i) (k := K) (n := n) hKle hp.pos
rw [hiff]
have hKmod : K % p ^ i = K := Nat.mod_eq_of_lt hKlt_pi
rw [hKmod]
have hdecomp : n % p ^ i = n % p ^ (i - 1) + p ^ (i - 1) * (n / p ^ (i - 1) % p) := by
simpa [hm1] using Nat.mod_pow_succ (x := n) (b := p) (k := i - 1)
have hr_lt : n % p ^ (i - 1) < p ^ (i - 1) := Nat.mod_lt n (Nat.pow_pos hp.pos)
have hl' : n / p ^ (i - 1) % p < p - 1 := hl
rw [hdecomp]
have hpos : 0 < p ^ (i - 1) := Nat.pow_pos hp.pos
have h1 : n % p ^ (i - 1) + p ^ (i - 1) * (n / p ^ (i - 1) % p) <
p ^ (i - 1) + p ^ (i - 1) * (n / p ^ (i - 1) % p) :=
Nat.add_lt_add_right hr_lt _
have h2 : p ^ (i - 1) + p ^ (i - 1) * (n / p ^ (i - 1) % p) =
p ^ (i - 1) * ((n / p ^ (i - 1) % p) + 1) := by ring
have h3 : p ^ (i - 1) * ((n / p ^ (i - 1) % p) + 1) ≤ p ^ (i - 1) * (p - 1) :=
Nat.mul_le_mul_left _ (by omega)
calc
n % p ^ (i - 1) + p ^ (i - 1) * (n / p ^ (i - 1) % p)
< p ^ (i - 1) * ((n / p ^ (i - 1) % p) + 1) := by simpa [h2] using h1
_ ≤ p ^ (i - 1) * (p - 1) := h3
_ = K := by simp only [K]; ring
exact Finset.mem_filter.mpr ⟨hi_mem, hcarry⟩
lemma truncated_number_properties {p n q m : ℕ} (hp : Nat.Prime p)
(hmpos : 0 < m) (hmle : m ≤ (Nat.digits p n).length)
(hspec : q ≤ ((Nat.digits p n).take m).sum)
(hmin : ¬ q ≤ ((Nat.digits p n).take (m - 1)).sum) :
let e := q - ((Nat.digits p n).take (m - 1)).sum
let S := n % p ^ (m - 1) + e * p ^ (m - 1)
alpha p S = q ∧ NoCarry p S n ∧ S ≤ n := by
let L := Nat.digits p n
let pre := L.take (m - 1)
let a := pre.sum
let d := L[m - 1]
let e := q - a
let r := n % p ^ (m - 1)
let S := r + e * p ^ (m - 1)
have hmle' : m ≤ L.length := hmle
have hmlt : m - 1 < L.length := by omega
have hprelen : pre.length = m - 1 := by
simp only [pre, List.length_take]
omega
have hm1 : m - 1 + 1 = m := Nat.sub_add_cancel hmpos
have hsum_take : (L.take m).sum = a + d := by
have hs := List.sum_take_succ L (m - 1) hmlt
simpa [a, d, pre, hm1] using hs
have hspec' : q ≤ (L.take m).sum := hspec
have ha_lt : a < q := by
have := hmin
simpa [a, pre] using this
have he_le_d : e ≤ d := by omega
have hd_lt : d < p := by
exact Nat.digits_lt_base hp.one_lt (List.getElem_mem hmlt)
have hr_lt : r < p ^ (m - 1) := Nat.mod_lt n (Nat.pow_pos hp.pos)
have htake_m : L.take m = pre ++ [d] := by
simp [pre, d, hm1]
have hpre_of : Nat.ofDigits p pre = r := by
change Nat.ofDigits p pre = n % p ^ (m - 1)
rw [Nat.self_mod_pow_eq_ofDigits_take (p := p) (i := m - 1) (n := n) hp.two_le]
have hnmodm : n % p ^ m = r + d * p ^ (m - 1) := by
rw [Nat.self_mod_pow_eq_ofDigits_take (p := p) (i := m) (n := n) hp.two_le]
rw [htake_m, Nat.ofDigits_append, Nat.ofDigits_singleton, hprelen, hpre_of]
ring
have hS_le_nmodm : S ≤ n % p ^ m := by
rw [hnmodm]
change r + e * p ^ (m - 1) ≤ r + d * p ^ (m - 1)
exact Nat.add_le_add_left (Nat.mul_le_mul_right (p ^ (m - 1)) he_le_d) r
have hS_lt_pm : S < p ^ m := by
have hlt : n % p ^ m < p ^ m := Nat.mod_lt n (Nat.pow_pos (n := m) hp.pos)
omega
have hS_mod_m1 : S % p ^ (m - 1) = r := by
change (r + e * p ^ (m - 1)) % p ^ (m - 1) = r
rw [Nat.add_mul_mod_self_right r e (p ^ (m - 1))]
exact Nat.mod_eq_of_lt hr_lt
have hdigits_sum : alpha p S = q := by
have he_lt : e < p := by omega
have hpre_lt : ∀ x ∈ pre, x < p := by
intro x hx
exact Nat.digits_lt_base hp.one_lt (List.take_subset (m - 1) L hx)
let L' := pre ++ [e]
have hL'_lt : ∀ x ∈ L', x < p := by
intro x hx
simp only [L', List.mem_append, List.mem_singleton] at hx
rcases hx with hx | rfl
· exact hpre_lt x hx
· exact he_lt
have hS_of : Nat.ofDigits p L' = S := by
rw [Nat.ofDigits_append, Nat.ofDigits_singleton, hprelen, hpre_of]
ring
have hsum_digits : (Nat.digits p S).sum = L'.sum := by
rw [← hS_of]
exact Nat.sum_digits_ofDigits_eq_sum hp.one_lt (l := L'.length) ⟨rfl, hL'_lt⟩
have hL'_sum : L'.sum = q := by
simp only [L', List.sum_append, List.sum_singleton]
omega
change (Nat.digits p S).sum = q
rw [hsum_digits, hL'_sum]
have hNoCarry : NoCarry p S n := by
change ∀ i : ℕ, S % p ^ i ≤ n % p ^ i
intro i
by_cases hi : i ≤ m - 1
· have hdvd : p ^ i ∣ p ^ (m - 1) := Nat.pow_dvd_pow p hi
have hS : S % p ^ i = r % p ^ i := by
have h := Nat.mod_mod_of_dvd S hdvd
rw [hS_mod_m1] at h
omega
have hn : n % p ^ i = r % p ^ i := by
exact (Nat.mod_mod_of_dvd n hdvd).symm
exact le_of_eq (hS.trans hn.symm)
· have him : m ≤ i := by omega
by_cases hieq : i = m
· subst i
change S % p ^ m ≤ n % p ^ m
rw [Nat.mod_eq_of_lt hS_lt_pm]
exact hS_le_nmodm
· have himlt : m < i := by omega
have hSmod : S % p ^ i = S := by
apply Nat.mod_eq_of_lt
have hpmi : p ^ m < p ^ i := Nat.pow_lt_pow_right hp.one_lt himlt
omega
have hnm : n % p ^ m ≤ n % p ^ i := by
have hdvd : p ^ m ∣ p ^ i := Nat.pow_dvd_pow p (by omega)
have h := Nat.mod_mod_of_dvd n hdvd
have hle := Nat.mod_le (n % p ^ i) (p ^ m)
rw [h] at hle
exact hle
change S % p ^ i ≤ n % p ^ i
rw [hSmod]
exact le_trans hS_le_nmodm hnm
exact ⟨hdigits_sum, hNoCarry, le_of_noCarry hp.one_lt hNoCarry⟩
lemma exists_truncated_noCarry_witness {p n q : ℕ} (hp : Nat.Prime p) (hq : 0 < q)
(hpq : p ≡ 1 [MOD q]) (halpha : q < alpha p n) :
∃ S : ℕ, 0 < S ∧ S < n ∧ q ∣ S ∧ NoCarry p S n := by
let L := Nat.digits p n
have hlen : q ≤ (L.take L.length).sum := by
simpa [L, alpha] using Nat.le_of_lt halpha
have hex : ∃ m, q ≤ (L.take m).sum := ⟨L.length, hlen⟩
let m := Nat.find hex
have hm_spec : q ≤ (L.take m).sum := Nat.find_spec hex
have hmle : m ≤ L.length := Nat.find_min' hex hlen
have hmpos : 0 < m := by
rw [Nat.find_pos hex]
simp only [List.take_zero, List.sum_nil, nonpos_iff_eq_zero]
exact hq.ne'
have hm_min : ¬ q ≤ (L.take (m - 1)).sum := Nat.find_min hex (by omega)
let e := q - (L.take (m - 1)).sum
let S := n % p ^ (m - 1) + e * p ^ (m - 1)
obtain ⟨halphaS, hNoCarry, hSle⟩ :=
truncated_number_properties hp hmpos hmle hm_spec hm_min
change alpha p S = q at halphaS
change NoCarry p S n at hNoCarry
change S ≤ n at hSle
have hS_ne : S ≠ n := by
intro heq
have hqeq : q = alpha p n := by rw [← halphaS, heq]
omega
have hS_lt : S < n := lt_of_le_of_ne hSle hS_ne
have hS_pos : 0 < S := by
have hne : S ≠ 0 := by
intro h0
have hzero : alpha p S = 0 := by simp [h0, alpha, Nat.digits_zero]
exact hq.ne' (halphaS.symm.trans hzero)
exact Nat.pos_of_ne_zero hne
have hq_dvd_S : q ∣ S := by
have hmod : alpha p S ≡ S [MOD q] := alpha_modEq hq hpq
have hmod0 : alpha p S ≡ 0 [MOD q] := by
rw [halphaS]
simp [Nat.ModEq]
have hS0 : S ≡ 0 [MOD q] := hmod.symm.trans hmod0
exact Nat.modEq_zero_iff_dvd.mp hS0
exact ⟨S, hS_pos, hS_lt, hq_dvd_S, hNoCarry⟩
lemma exists_padicValNat_choose_eq_one {p n q : ℕ} (hp : Nat.Prime p) (hq : 0 < q)
(hpq : p ≡ 1 [MOD q]) (hnq : q < n) (halpha : alpha p n ≤ q) :
∃ k : ℕ, 0 < k ∧ q * k < n ∧ padicValNat p (n.choose (q * k)) = 1 := by
let i := Nat.log p n
have hn : 0 < n := by omega
have hi_pos : 0 < i := by
simpa [i] using top_exp_pos_of_alpha_le hp hnq halpha
have hlen : (Nat.digits p n).length = i + 1 := by
simpa [i] using Nat.length_digits p n hp.one_lt hn.ne'
have hi : i < (Nat.digits p n).length := by omega
have hl : n / p ^ (i - 1) % p < p - 1 :=
top_pred_digit_lt hp hq hpq hnq halpha rfl hi_pos hi
let K := (p - 1) * p ^ (i - 1)
have hKlt_pi : K < p ^ i := by
simpa [K] using pred_mul_pow_lt_pow hp.one_lt hi_pos
have hpi_le : p ^ i ≤ n := by
simpa [i] using Nat.pow_log_le_self p hn.ne'
have hKlt : K < n := by omega
have hKpos : 0 < K := by
exact Nat.mul_pos (by omega) (Nat.pow_pos hp.pos)
have hq_dvd_p : q ∣ p - 1 := q_dvd_p_sub_one_of_prime_modEq hq hpq
have hqK : q ∣ K := dvd_mul_of_dvd_left hq_dvd_p _
let k := K / q
have hqk : q * k = K := by
simpa [k] using Nat.mul_div_cancel' hqK
have hq_le_K : q ≤ K := Nat.le_of_dvd hKpos hqK
have hkpos : 0 < k := by
simpa [k] using Nat.div_pos hq_le_K hq
have hkn : q * k < n := by
rw [hqk]
exact hKlt
have hv : padicValNat p (n.choose K) = 1 := by
have : Fact (Nat.Prime p) := ⟨hp⟩
have hlog_lt : Nat.log p n < i + 1 := by simp [i]
rw [padicValNat_choose (p := p) (n := n) (k := K) (b := i + 1)
(by omega : K ≤ n) hlog_lt,
kummer_filter_eq_singleton hp hi_pos hl hKlt]
simp
refine ⟨k, hkpos, hkn, ?_⟩
simpa [hqk] using hv
lemma padicValNat_choose_pos_of_alpha_le {p n q k : ℕ} (hp : Nat.Prime p) (hq : 0 < q)
(hpq : p ≡ 1 [MOD q]) (halpha : alpha p n ≤ q)
(hk0 : 0 < k) (hkn : q * k < n) :
0 < padicValNat p (n.choose (q * k)) := by
by_contra hpos
have hzero : padicValNat p (n.choose (q * k)) = 0 := Nat.eq_zero_of_not_pos hpos
have hlt : alpha p (q * k) < alpha p n :=
alpha_lt_of_choose_eq_zero hp hkn hzero
have hq_dvd_alpha : q ∣ alpha p (q * k) :=
Nat.modEq_zero_iff_dvd.mp ((alpha_modEq hq hpq).trans
(Nat.modEq_zero_iff_dvd.mpr (Nat.dvd_mul_right q k)))
have hq_le_alpha : q ≤ alpha p (q * k) :=
Nat.le_of_dvd (alpha_pos_of_pos (Nat.mul_pos hq hk0)) hq_dvd_alpha
omega
lemma padicValNat_gcd_of_choose_le_choose {p n q k : ℕ} (hp : Nat.Prime p)
(hq : 0 < q) (hk : k ∈ choose_k_set n q) :
padicValNat p (gcd_of_choose n q) ≤ padicValNat p (n.choose (q * k)) := by
have : Fact (Nat.Prime p) := ⟨hp⟩
have hkn : q * k < n := ((mem_choose_k_set hq).mp hk).2
have hcne : n.choose (q * k) ≠ 0 := Nat.choose_ne_zero hkn.le
have hcdvd : p ^ padicValNat p (gcd_of_choose n q) ∣ n.choose (q * k) :=
dvd_trans pow_padicValNat_dvd (Finset.gcd_dvd hk)
exact
(padicValNat_dvd_iff_le (p := p) (a := n.choose (q * k))
(n := padicValNat p (gcd_of_choose n q)) hcne).mp hcdvd
lemma padicValNat_gcd_of_choose_eq_one {p n q : ℕ} (hp : Nat.Prime p) (hq : 0 < q)
(hnq : q < n)
(hforall : ∀ k ∈ choose_k_set n q, 0 < padicValNat p (n.choose (q * k)))
(hone : ∃ k ∈ choose_k_set n q, padicValNat p (n.choose (q * k)) = 1) :
padicValNat p (gcd_of_choose n q) = 1 := by
have : Fact (Nat.Prime p) := ⟨hp⟩
have hgne : gcd_of_choose n q ≠ 0 := gcd_of_choose_ne_zero hq hnq
have hupper : padicValNat p (gcd_of_choose n q) ≤ 1 := by
obtain ⟨k, hk, hv⟩ := hone
have hle := padicValNat_gcd_of_choose_le_choose hp hq hk
omega
have hlower : 1 ≤ padicValNat p (gcd_of_choose n q) := by
have hpdvd_each : ∀ k ∈ choose_k_set n q, p ∣ n.choose (q * k) := by
intro k hk
have hvpos := hforall k hk
exact dvd_trans (dvd_pow_self p hvpos.ne') pow_padicValNat_dvd
exact one_le_padicValNat_of_dvd hgne (Finset.dvd_gcd hpdvd_each)
omega
lemma padicValNat_gcd_of_choose_eq_zero {p n q : ℕ} (hp : Nat.Prime p) (hq : 0 < q)
(hzero : ∃ k ∈ choose_k_set n q, padicValNat p (n.choose (q * k)) = 0) :
padicValNat p (gcd_of_choose n q) = 0 := by
obtain ⟨k, hk, hv⟩ := hzero
have hle := padicValNat_gcd_of_choose_le_choose hp hq hk
omega
theorem theoremQ_if {n q p : ℕ} (hq : 0 < q) (hnq : q < n) (hp : Nat.Prime p)
(hpq : p ≡ 1 [MOD q]) (halpha : alpha p n ≤ q) :
padicValNat p (gcd_of_choose n q) = 1 := by
apply padicValNat_gcd_of_choose_eq_one hp hq hnq
· intro k hk
have hk0 : 0 < k := ((mem_choose_k_set hq).mp hk).1
have hkn : q * k < n := ((mem_choose_k_set hq).mp hk).2
exact padicValNat_choose_pos_of_alpha_le hp hq hpq halpha hk0 hkn
· obtain ⟨k, hk0, hkn, hv⟩ :=
exists_padicValNat_choose_eq_one hp hq hpq hnq halpha
exact ⟨k, (mem_choose_k_set hq).mpr ⟨hk0, hkn⟩, hv⟩
theorem theoremQ_onlyIf {n q p : ℕ} (hq : 0 < q) (hp : Nat.Prime p)
(hpq : p ≡ 1 [MOD q]) (halpha : q < alpha p n) :
padicValNat p (gcd_of_choose n q) = 0 := by
apply padicValNat_gcd_of_choose_eq_zero hp hq
obtain ⟨S, hSpos, hSlt, hqS, hNoCarry⟩ :=
exists_truncated_noCarry_witness hp hq hpq halpha
let k := S / q
have hqk : q * k = S := by
simpa [k] using Nat.mul_div_cancel' hqS
have hq_le_S : q ≤ S := Nat.le_of_dvd hSpos hqS
have hk0 : 0 < k := by
simpa [k] using Nat.div_pos hq_le_S hq
have hkn : q * k < n := by
rw [hqk]
exact hSlt
have hv : padicValNat p (n.choose S) = 0 :=
(padicValNat_choose_eq_zero_iff_noCarry hp (by omega : S ≤ n)).mpr hNoCarry
have hv' : padicValNat p (n.choose (q * k)) = 0 := by
simpa [hqk] using hv
exact ⟨k, (mem_choose_k_set hq).mpr ⟨hk0, hkn⟩, hv'⟩
theorem theoremQ {n q p : ℕ} (hq : 0 < q) (hnq : q < n) (hp : Nat.Prime p)
(hpq : p ≡ 1 [MOD q]) :
padicValNat p (gcd_of_choose n q) = if alpha p n ≤ q then 1 else 0 := by
split_ifs with halpha
· exact theoremQ_if hq hnq hp hpq halpha
· exact theoremQ_onlyIf hq hp hpq (by omega)