Lean / Reader
Lean readerErdos824Grading.lean
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.
import Erdos824Witnessesimport Mathlib.Data.Nat.Factorization.Basicimport Mathlib.Data.Nat.ModEqnamespace Erdos824Gradingopen Erdos824Witnesses/-- A nonnegative integer linear combination of prime valuations, evaluated at n. -/def grade : List (ℕ × ℕ) → ℕ → ℕ| [], _ => 0| (p,w)::ms,n => w*n.factorization p + grade ms ntheorem grade_one (ms : List (ℕ × ℕ)) : grade ms 1 = 0 := byinduction ms with| nil => rfl| cons pw ms ih => simp [grade, ih]theorem grade_mul (ms : List (ℕ × ℕ)) {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) :grade ms (a*b) = grade ms a + grade ms b := byinduction ms with| nil => rfl| cons pw ms ih =>obtain ⟨p,w⟩ := pwsimp only [grade, Nat.factorization_mul ha hb, Finsupp.add_apply, ih]ringprivate theorem values_pos (ps : List ℕ) {s : ℕ} (hs : s ∈ values ps) : 0 < s := byinduction ps generalizing s with| nil =>have he : s=1 := by simpa [values] using hsomega| cons p ps ih =>rcases List.mem_append.mp hs with h | h· exact ih h· obtain ⟨t,ht,rfl⟩ := List.mem_map.mp hexact Nat.mul_pos (Nat.succ_pos p) (ih ht)theorem graded_subset (ms : List (ℕ × ℕ)) (k c : ℕ) {ps : List ℕ}(hv : ∀ p∈ps, Nat.ModEq k (grade ms (p+1)) c){s : ℕ} (hs : s ∈ values ps) :∃ t ≤ ps.length, Nat.ModEq k (grade ms s) (t*c) ∧ (t=0 → s=1) ∧(t=ps.length → s=shiftProduct ps) := byinduction ps generalizing s with| nil =>have he : s=1 := by simpa [values] using hssubst sexact ⟨0,by simp,by simp [grade_one, Nat.ModEq],by simp,by simp [shiftProduct]⟩| cons p ps ih =>have ht : ∀ q∈ps, Nat.ModEq k (grade ms (q+1)) c := fun q hq => hv q (.tail p hq)rcases List.mem_append.mp hs with h | h· obtain ⟨t,hl,hm,hz,_⟩ := ih ht hexact ⟨t,by simp; omega,hm,hz,by simp; omega⟩· obtain ⟨v,hv',rfl⟩ := List.mem_map.mp hobtain ⟨t,hl,hm,_,he⟩ := ih ht hv'refine ⟨t+1,by simpa using hl,?_,by omega,?_⟩· rw [grade_mul _ (Nat.succ_ne_zero _) (values_pos _ hv').ne', Nat.add_mul, one_mul]simpa [Nat.add_comm] using ((hv p (.head ps)).add hm)· intro hlenhave hte : t=ps.length := by simpa using hlenrw [he hte]rfl/-- A count of k factors, each of grade one, can match grade zero only whenthe full list or the empty list was used. -/theorem grading_excludes_partial (ms : List (ℕ × ℕ)) {left right : List ℕ}(hl : ∀ p∈left, Nat.ModEq left.length (grade ms (p+1)) 1)(hr : ∀ p∈right, Nat.ModEq left.length (grade ms (p+1)) 0){s : ℕ} (hs : s∈values left) (ht : s∈values right) :s=1 ∨ s=shiftProduct left := byobtain ⟨t,hsize,hm,hzero,hfull⟩ := graded_subset ms left.length 1 hl hsobtain ⟨u,_,hm',_,_⟩ := graded_subset ms left.length 0 hr htsimp only [Nat.mul_one] at hmsimp only [Nat.mul_zero] at hm'have hd := Nat.modEq_zero_iff_dvd.mp (hm.symm.trans hm')by_cases hz : t=0· exact Or.inl (hzero hz)· exact Or.inr (hfull (le_antisymm hsize (Nat.le_of_dvd (Nat.pos_of_ne_zero hz) hd)))private def residueScore : List (ℕ × ℕ) → ℕ → ℕ| [],_ => 0| (p,w)::ms,n => (if p ∣ n then w else 0) + residueScore ms nprivate theorem score_eq {ms : List (ℕ × ℕ)} {n : ℕ} (hn : n ≠ 0)(hp : ∀ pw∈ms, Nat.Prime pw.1) (hs : ∀ pw∈ms, ¬pw.1^2 ∣ n) :grade ms n = residueScore ms n := byinduction ms with| nil => rfl| cons pw ms ih =>obtain ⟨p,w⟩ := pwhave hprime := hp (p,w) (.head ms)have hsq := hs (p,w) (.head ms)have htail := ih (fun q h => hp q (.tail _ h)) (fun q h => hs q (.tail _ h))simp only [grade, residueScore, htail]by_cases h : p ∣ n· have htwo : ¬2 ≤ n.factorization p := fun hle => hsq ((hprime.pow_dvd_iff_le_factorization hn).2 hle)have hone : 1 ≤ n.factorization p :=(hprime.pow_dvd_iff_le_factorization hn).1 (by simpa using h)have he : n.factorization p=1 := by omegasimp [h,he]· simp [h,Nat.factorization_eq_zero_of_not_dvd h]def gradeTest (ms : List (ℕ × ℕ)) (k c : ℕ) (ps : List ℕ) : Bool :=ps.all fun p => ms.all (fun q => decide (¬q.1^2 ∣ p+1)) &&decide (residueScore ms (p+1) % k = c % k)theorem gradeTest_sound {ms : List (ℕ × ℕ)} {k c : ℕ} {ps : List ℕ}(hp : (ms.all (fun q => decide (Nat.Prime q.1))) = true)(h : gradeTest ms k c ps = true) : ∀ p∈ps, Nat.ModEq k (grade ms (p+1)) c := byintro p hmemobtain ⟨hs,he⟩ := Bool.and_eq_true_iff.mp (List.all_eq_true.mp h p hmem)have heq := score_eq (n:=p+1) (Nat.succ_ne_zero _)(fun q hq => of_decide_eq_true (List.all_eq_true.mp hp q hq))(fun q hq => of_decide_eq_true (List.all_eq_true.mp hs q hq))rw [heq]exact of_decide_eq_true he#print axioms gradeTest_sound#print axioms grading_excludes_partialend Erdos824Grading