Lean / janjarosz05/0kwas / Erdos1094Rule.lean
Lean reader

Kwas/Erdos1094Rule.lean

92 lines6 declarationsno sorryimports Mathlib
  1. import Mathlib
  2. /-!
  3. # Erdős Problem 1094: the kill rule of the logic net, proved
  4. The logic net learned, from pairs with `n ≤ 120`, that clock `p` lets `(n, k)` through exactly when every
  5. base-`p` digit of `k` is at most the corresponding digit of `n`. Here this rule is proved for all `n, k`
  6. (it is Lucas's theorem in digit form), and the exceptional set of the problem is rewritten with it.
  7. -/
  8. namespace Erdos1094Rule
  9. /-- The learned gate, applied to all base-`p` digit pairs: every digit of `k` is `≤` that of `n`. -/
  10. def passes (p : ℕ) : ℕ → ℕ → Prop
  11. | n, k => if _h : k = 0 ∨ p < 2 then True else k % p ≤ n % p ∧ passes p (n / p) (k / p)
  12. decreasing_by
  13. have hk : k ≠ 0 := fun h0 ↦ _h (Or.inl h0)
  14. have hp : 2 ≤ p := by omega
  15. exact Nat.div_lt_self (Nat.pos_of_ne_zero hk) hp
  16. theorem passes_zero (p n : ℕ) : passes p n 0 := by
  17. unfold passes; simp
  18. theorem passes_step {p : ℕ} (hp : 2 ≤ p) (n : ℕ) {k : ℕ} (hk : k ≠ 0) :
  19. passes p n k ↔ k % p ≤ n % p ∧ passes p (n / p) (k / p) := by
  20. rw [passes]
  21. have h1 : 1 < p := by omega
  22. simp [hk, h1]
  23. /-- **Kill rule.** For a prime `p`: `p ∤ C(n, k)` iff every base-`p` digit of `k` is at most that of `n`. -/
  24. theorem not_dvd_choose_iff_passes {p : ℕ} (hp : p.Prime) :
  25. ∀ k n : ℕ, ¬ p ∣ n.choose k ↔ passes p n k := by
  26. have : Fact p.Prime := ⟨hp⟩
  27. intro k
  28. induction k using Nat.strong_induction_on with
  29. | _ k ih =>
  30. intro n
  31. by_cases hk : k = 0
  32. · subst hk
  33. simp [passes_zero, hp.one_lt.ne', Nat.dvd_one]
  34. · rw [passes_step hp.two_le n hk]
  35. have hlt : k / p < k := Nat.div_lt_self (Nat.pos_of_ne_zero hk) hp.two_le
  36. rw [← ih (k / p) hlt (n / p)]
  37. have hl := Choose.choose_modEq_choose_mod_mul_choose_div_nat (n := n) (k := k) (p := p)
  38. have hdvd : p ∣ n.choose k ↔ p ∣ (n % p).choose (k % p) * (n / p).choose (k / p) := by
  39. rw [Nat.dvd_iff_mod_eq_zero, Nat.dvd_iff_mod_eq_zero]
  40. unfold Nat.ModEq at hl
  41. rw [hl]
  42. rw [hdvd, hp.dvd_mul, not_or]
  43. have hdig : ¬ p ∣ (n % p).choose (k % p) ↔ k % p ≤ n % p := by
  44. constructor
  45. · intro h
  46. by_contra hlt'
  47. exact h (by rw [Nat.choose_eq_zero_of_lt (by omega)]; exact dvd_zero p)
  48. · intro hle hd
  49. have hnp : n % p < p := Nat.mod_lt _ hp.pos
  50. have hpos : 0 < (n % p).choose (k % p) := Nat.choose_pos hle
  51. have : p ∣ (n % p).factorial := by
  52. rw [← Nat.choose_mul_factorial_mul_factorial hle]
  53. exact Dvd.dvd.mul_right (Dvd.dvd.mul_right hd _) _
  54. exact absurd ((hp.dvd_factorial).1 this) (by omega)
  55. rw [hdig]
  56. /-- The exceptional set of Erdős Problem 1094 (verbatim from Formal Conjectures). -/
  57. def Exc : Set (ℕ × ℕ) :=
  58. {(n, k) : ℕ × ℕ | 0 < k ∧ 2 * k ≤ n ∧ (n.choose k).minFac > max (n / k) k}
  59. /-- A pair is exceptional iff it passes every prime clock `p ≤ max (n / k) k`. -/
  60. theorem mem_Exc_iff {n k : ℕ} (hk : 0 < k) (hn : 2 * k ≤ n) :
  61. (n, k) ∈ Exc ↔ ∀ p : ℕ, p.Prime → p ≤ max (n / k) k → passes p n k := by
  62. have hc1 : n.choose k ≠ 1 := by
  63. intro h
  64. have := Nat.choose_eq_one_iff.1 h
  65. omega
  66. constructor
  67. · rintro ⟨-, -, h⟩ p hp hple
  68. rw [← not_dvd_choose_iff_passes hp]
  69. intro hd
  70. have := Nat.minFac_le_of_dvd hp.two_le hd
  71. omega
  72. · intro h
  73. refine ⟨hk, hn, ?_⟩
  74. by_contra hle
  75. push Not at hle
  76. have hp := Nat.minFac_prime hc1
  77. exact (not_dvd_choose_iff_passes hp k n).2 (h _ hp hle) (Nat.minFac_dvd _)
  78. end Erdos1094Rule
  79. #print axioms Erdos1094Rule.not_dvd_choose_iff_passes
  80. #print axioms Erdos1094Rule.mem_Exc_iff