import Mathlib/-!# Erdős Problem 1094: the kill rule of the logic net, provedThe logic net learned, from pairs with `n ≤ 120`, that clock `p` lets `(n, k)` through exactly when everybase-`p` digit of `k` is at most the corresponding digit of `n`. Here this rule is proved for all `n, k`(it is Lucas's theorem in digit form), and the exceptional set of the problem is rewritten with it.-/namespace Erdos1094Rule/-- The learned gate, applied to all base-`p` digit pairs: every digit of `k` is `≤` that of `n`. -/def passes (p : ℕ) : ℕ → ℕ → Prop| n, k => if _h : k = 0 ∨ p < 2 then True else k % p ≤ n % p ∧ passes p (n / p) (k / p)decreasing_byhave hk : k ≠ 0 := fun h0 ↦ _h (Or.inl h0)have hp : 2 ≤ p := by omegaexact Nat.div_lt_self (Nat.pos_of_ne_zero hk) hptheorem passes_zero (p n : ℕ) : passes p n 0 := byunfold passes; simptheorem passes_step {p : ℕ} (hp : 2 ≤ p) (n : ℕ) {k : ℕ} (hk : k ≠ 0) :passes p n k ↔ k % p ≤ n % p ∧ passes p (n / p) (k / p) := byrw [passes]have h1 : 1 < p := by omegasimp [hk, h1]/-- **Kill rule.** For a prime `p`: `p ∤ C(n, k)` iff every base-`p` digit of `k` is at most that of `n`. -/theorem not_dvd_choose_iff_passes {p : ℕ} (hp : p.Prime) :∀ k n : ℕ, ¬ p ∣ n.choose k ↔ passes p n k := byhave : Fact p.Prime := ⟨hp⟩intro kinduction k using Nat.strong_induction_on with| _ k ih =>intro nby_cases hk : k = 0· subst hksimp [passes_zero, hp.one_lt.ne', Nat.dvd_one]· rw [passes_step hp.two_le n hk]have hlt : k / p < k := Nat.div_lt_self (Nat.pos_of_ne_zero hk) hp.two_lerw [← ih (k / p) hlt (n / p)]have hl := Choose.choose_modEq_choose_mod_mul_choose_div_nat (n := n) (k := k) (p := p)have hdvd : p ∣ n.choose k ↔ p ∣ (n % p).choose (k % p) * (n / p).choose (k / p) := byrw [Nat.dvd_iff_mod_eq_zero, Nat.dvd_iff_mod_eq_zero]unfold Nat.ModEq at hlrw [hl]rw [hdvd, hp.dvd_mul, not_or]have hdig : ¬ p ∣ (n % p).choose (k % p) ↔ k % p ≤ n % p := byconstructor· intro hby_contra hlt'exact h (by rw [Nat.choose_eq_zero_of_lt (by omega)]; exact dvd_zero p)· intro hle hdhave hnp : n % p < p := Nat.mod_lt _ hp.poshave hpos : 0 < (n % p).choose (k % p) := Nat.choose_pos hlehave : p ∣ (n % p).factorial := byrw [← Nat.choose_mul_factorial_mul_factorial hle]exact Dvd.dvd.mul_right (Dvd.dvd.mul_right hd _) _exact absurd ((hp.dvd_factorial).1 this) (by omega)rw [hdig]/-- The exceptional set of Erdős Problem 1094 (verbatim from Formal Conjectures). -/def Exc : Set (ℕ × ℕ) :={(n, k) : ℕ × ℕ | 0 < k ∧ 2 * k ≤ n ∧ (n.choose k).minFac > max (n / k) k}/-- A pair is exceptional iff it passes every prime clock `p ≤ max (n / k) k`. -/theorem mem_Exc_iff {n k : ℕ} (hk : 0 < k) (hn : 2 * k ≤ n) :(n, k) ∈ Exc ↔ ∀ p : ℕ, p.Prime → p ≤ max (n / k) k → passes p n k := byhave hc1 : n.choose k ≠ 1 := byintro hhave := Nat.choose_eq_one_iff.1 homegaconstructor· rintro ⟨-, -, h⟩ p hp hplerw [← not_dvd_choose_iff_passes hp]intro hdhave := Nat.minFac_le_of_dvd hp.two_le hdomega· intro hrefine ⟨hk, hn, ?_⟩by_contra hlepush Not at hlehave hp := Nat.minFac_prime hc1exact (not_dvd_choose_iff_passes hp k n).2 (h _ hp hle) (Nat.minFac_dvd _)end Erdos1094Rule#print axioms Erdos1094Rule.not_dvd_choose_iff_passes#print axioms Erdos1094Rule.mem_Exc_iff