Lean / Reader
Lean reader

Erdos824Grading.lean

120 lines10 declarationsno sorryimports Erdos824Witnesses Mathlib.Data.Nat.Factorization.Basic Mathlib.Data.Nat.ModEq

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports Erdos824Witnesses, which it does not have: you can read it there, but it will not check. Build the project itself to check it.

  1. import Erdos824Witnesses
  2. import Mathlib.Data.Nat.Factorization.Basic
  3. import Mathlib.Data.Nat.ModEq
  4. namespace Erdos824Grading
  5. open Erdos824Witnesses
  6. /-- A nonnegative integer linear combination of prime valuations, evaluated at n. -/
  7. def grade : List (ℕ × ℕ) → ℕ → ℕ
  8. | [], _ => 0
  9. | (p,w)::ms,n => w*n.factorization p + grade ms n
  10. theorem grade_one (ms : List (ℕ × ℕ)) : grade ms 1 = 0 := by
  11. induction ms with
  12. | nil => rfl
  13. | cons pw ms ih => simp [grade, ih]
  14. theorem grade_mul (ms : List (ℕ × ℕ)) {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) :
  15. grade ms (a*b) = grade ms a + grade ms b := by
  16. induction ms with
  17. | nil => rfl
  18. | cons pw ms ih =>
  19. obtain ⟨p,w⟩ := pw
  20. simp only [grade, Nat.factorization_mul ha hb, Finsupp.add_apply, ih]
  21. ring
  22. private theorem values_pos (ps : List ℕ) {s : ℕ} (hs : s ∈ values ps) : 0 < s := by
  23. induction ps generalizing s with
  24. | nil =>
  25. have he : s=1 := by simpa [values] using hs
  26. omega
  27. | cons p ps ih =>
  28. rcases List.mem_append.mp hs with h | h
  29. · exact ih h
  30. · obtain ⟨t,ht,rfl⟩ := List.mem_map.mp h
  31. exact Nat.mul_pos (Nat.succ_pos p) (ih ht)
  32. theorem graded_subset (ms : List (ℕ × ℕ)) (k c : ℕ) {ps : List ℕ}
  33. (hv : ∀ p∈ps, Nat.ModEq k (grade ms (p+1)) c)
  34. {s : ℕ} (hs : s ∈ values ps) :
  35. ∃ t ≤ ps.length, Nat.ModEq k (grade ms s) (t*c) ∧ (t=0 → s=1) ∧
  36. (t=ps.length → s=shiftProduct ps) := by
  37. induction ps generalizing s with
  38. | nil =>
  39. have he : s=1 := by simpa [values] using hs
  40. subst s
  41. exact ⟨0,by simp,by simp [grade_one, Nat.ModEq],by simp,by simp [shiftProduct]⟩
  42. | cons p ps ih =>
  43. have ht : ∀ q∈ps, Nat.ModEq k (grade ms (q+1)) c := fun q hq => hv q (.tail p hq)
  44. rcases List.mem_append.mp hs with h | h
  45. · obtain ⟨t,hl,hm,hz,_⟩ := ih ht h
  46. exact ⟨t,by simp; omega,hm,hz,by simp; omega⟩
  47. · obtain ⟨v,hv',rfl⟩ := List.mem_map.mp h
  48. obtain ⟨t,hl,hm,_,he⟩ := ih ht hv'
  49. refine ⟨t+1,by simpa using hl,?_,by omega,?_⟩
  50. · rw [grade_mul _ (Nat.succ_ne_zero _) (values_pos _ hv').ne', Nat.add_mul, one_mul]
  51. simpa [Nat.add_comm] using ((hv p (.head ps)).add hm)
  52. · intro hlen
  53. have hte : t=ps.length := by simpa using hlen
  54. rw [he hte]
  55. rfl
  56. /-- A count of k factors, each of grade one, can match grade zero only when
  57. the full list or the empty list was used. -/
  58. theorem grading_excludes_partial (ms : List (ℕ × ℕ)) {left right : List ℕ}
  59. (hl : ∀ p∈left, Nat.ModEq left.length (grade ms (p+1)) 1)
  60. (hr : ∀ p∈right, Nat.ModEq left.length (grade ms (p+1)) 0)
  61. {s : ℕ} (hs : s∈values left) (ht : s∈values right) :
  62. s=1 ∨ s=shiftProduct left := by
  63. obtain ⟨t,hsize,hm,hzero,hfull⟩ := graded_subset ms left.length 1 hl hs
  64. obtain ⟨u,_,hm',_,_⟩ := graded_subset ms left.length 0 hr ht
  65. simp only [Nat.mul_one] at hm
  66. simp only [Nat.mul_zero] at hm'
  67. have hd := Nat.modEq_zero_iff_dvd.mp (hm.symm.trans hm')
  68. by_cases hz : t=0
  69. · exact Or.inl (hzero hz)
  70. · exact Or.inr (hfull (le_antisymm hsize (Nat.le_of_dvd (Nat.pos_of_ne_zero hz) hd)))
  71. private def residueScore : List (ℕ × ℕ) → ℕ → ℕ
  72. | [],_ => 0
  73. | (p,w)::ms,n => (if p ∣ n then w else 0) + residueScore ms n
  74. private theorem score_eq {ms : List (ℕ × ℕ)} {n : ℕ} (hn : n ≠ 0)
  75. (hp : ∀ pw∈ms, Nat.Prime pw.1) (hs : ∀ pw∈ms, ¬pw.1^2 ∣ n) :
  76. grade ms n = residueScore ms n := by
  77. induction ms with
  78. | nil => rfl
  79. | cons pw ms ih =>
  80. obtain ⟨p,w⟩ := pw
  81. have hprime := hp (p,w) (.head ms)
  82. have hsq := hs (p,w) (.head ms)
  83. have htail := ih (fun q h => hp q (.tail _ h)) (fun q h => hs q (.tail _ h))
  84. simp only [grade, residueScore, htail]
  85. by_cases h : p ∣ n
  86. · have htwo : ¬2 ≤ n.factorization p := fun hle => hsq ((hprime.pow_dvd_iff_le_factorization hn).2 hle)
  87. have hone : 1 ≤ n.factorization p :=
  88. (hprime.pow_dvd_iff_le_factorization hn).1 (by simpa using h)
  89. have he : n.factorization p=1 := by omega
  90. simp [h,he]
  91. · simp [h,Nat.factorization_eq_zero_of_not_dvd h]
  92. def gradeTest (ms : List (ℕ × ℕ)) (k c : ℕ) (ps : List ℕ) : Bool :=
  93. ps.all fun p => ms.all (fun q => decide (¬q.1^2 ∣ p+1)) &&
  94. decide (residueScore ms (p+1) % k = c % k)
  95. theorem gradeTest_sound {ms : List (ℕ × ℕ)} {k c : ℕ} {ps : List ℕ}
  96. (hp : (ms.all (fun q => decide (Nat.Prime q.1))) = true)
  97. (h : gradeTest ms k c ps = true) : ∀ p∈ps, Nat.ModEq k (grade ms (p+1)) c := by
  98. intro p hmem
  99. obtain ⟨hs,he⟩ := Bool.and_eq_true_iff.mp (List.all_eq_true.mp h p hmem)
  100. have heq := score_eq (n:=p+1) (Nat.succ_ne_zero _)
  101. (fun q hq => of_decide_eq_true (List.all_eq_true.mp hp q hq))
  102. (fun q hq => of_decide_eq_true (List.all_eq_true.mp hs q hq))
  103. rw [heq]
  104. exact of_decide_eq_true he
  105. #print axioms gradeTest_sound
  106. #print axioms grading_excludes_partial
  107. end Erdos824Grading