import Mathlib.NumberTheory.Divisors import Mathlib.Data.Finset.Max import Mathlib.Tactic namespace Math.DivisorSequence open Finset /-- Helper: membership in the double-erase set gives membership in `properDivisors` and non-equality with the erased elements. -/ lemma mem_erase_erase_iff {n b : ℕ} : b ∈ ((n.properDivisors).erase (n/2) |>.erase (n/3)) ↔ b ∈ n.properDivisors ∧ b ≠ n/2 ∧ b ≠ n/3 := by classical constructor · intro hb have hb1 : b ≠ n/3 ∧ b ∈ (n.properDivisors).erase (n/2) := by simpa [Finset.mem_erase, and_left_comm, and_assoc, and_comm] using (Finset.mem_erase.1 hb) have hb2 : b ≠ n/2 ∧ b ∈ n.properDivisors := by simpa [Finset.mem_erase, and_left_comm, and_assoc, and_comm] using (Finset.mem_erase.1 hb1.2) exact ⟨hb2.2, hb2.1, hb1.1⟩ · rintro ⟨hbpd, hbne2, hbne3⟩ have : b ∈ (n.properDivisors).erase (n/2) := by exact (Finset.mem_erase.2 ⟨hbne2, hbpd⟩) exact (Finset.mem_erase.2 ⟨hbne3, this⟩) /-- The third-largest proper divisor in the main divisor-ordering situation. Assume `6 ∣ n`, `¬ 5 ∣ n`, `1 < n`. Let `s = n.properDivisors \ {n/2,n/3}` (implemented via two `erase`). Then * if `4 ∣ n` then `max' s = n/4`; * if `¬ 4 ∣ n` then `max' s = n/6`. This isolates the final case split needed to identify the top three proper divisors. -/ theorem max'_erase_div_two_erase_div_three_eq {n : ℕ} (h6 : 6 ∣ n) (h5 : ¬ 5 ∣ n) (hn1 : 1 < n) : ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' (by classical by_cases h4 : 4 ∣ n · refine ⟨n/4, ?_⟩ have hn0 : n ≠ 0 := Nat.ne_of_gt (lt_trans Nat.zero_lt_one hn1) have h3 : 3 ∣ n := dvd_trans (by decide : 3 ∣ 6) h6 have h2 : 2 ∣ n := dvd_trans (by decide : 2 ∣ 6) h6 have hlt43 : n/4 < n/3 := by have := (Nat.div_lt_div_left (a := n) (b := 4) (c := 3) hn0 h4 h3) simpa using this.2 (by decide : (3:ℕ) < 4) have hlt32 : n/3 < n/2 := by have := (Nat.div_lt_div_left (a := n) (b := 3) (c := 2) hn0 h3 h2) simpa using this.2 (by decide : (2:ℕ) < 3) have hne2 : n/4 ≠ n/2 := ne_of_lt (lt_trans hlt43 hlt32) have hne3 : n/4 ≠ n/3 := ne_of_lt hlt43 -- membership in properDivisors have hmem_pd : n/4 ∈ n.properDivisors := by refine (Nat.mem_properDivisors).2 ?_ constructor · exact Nat.div_dvd_of_dvd h4 · exact Nat.div_lt_self (Nat.pos_of_ne_zero hn0) (by decide : 1 < 4) exact (mem_erase_erase_iff (n := n) (b := n/4)).2 ⟨hmem_pd, hne2, hne3⟩ · refine ⟨n/6, ?_⟩ have hn0 : n ≠ 0 := Nat.ne_of_gt (lt_trans Nat.zero_lt_one hn1) have h3 : 3 ∣ n := dvd_trans (by decide : 3 ∣ 6) h6 have h2 : 2 ∣ n := dvd_trans (by decide : 2 ∣ 6) h6 have hlt63 : n/6 < n/3 := by have := (Nat.div_lt_div_left (a := n) (b := 6) (c := 3) hn0 h6 h3) simpa using this.2 (by decide : (3:ℕ) < 6) have hlt32 : n/3 < n/2 := by have := (Nat.div_lt_div_left (a := n) (b := 3) (c := 2) hn0 h3 h2) simpa using this.2 (by decide : (2:ℕ) < 3) have hne2 : n/6 ≠ n/2 := ne_of_lt (lt_trans hlt63 hlt32) have hne3 : n/6 ≠ n/3 := ne_of_lt hlt63 have hmem_pd : n/6 ∈ n.properDivisors := by refine (Nat.mem_properDivisors).2 ?_ constructor · exact Nat.div_dvd_of_dvd h6 · exact Nat.div_lt_self (Nat.pos_of_ne_zero hn0) (by decide : 1 < 6) exact (mem_erase_erase_iff (n := n) (b := n/6)).2 ⟨hmem_pd, hne2, hne3⟩ ) = if 4 ∣ n then n/4 else n/6 := by classical by_cases h4 : 4 ∣ n · -- show `max'` equals `n/4` have hn0 : n ≠ 0 := Nat.ne_of_gt (lt_trans Nat.zero_lt_one hn1) -- membership witness have hmem : n/4 ∈ ((n.properDivisors).erase (n/2) |>.erase (n/3)) := by -- reuse the same proof as in the Nonempty construction (redo quickly) have h3 : 3 ∣ n := dvd_trans (by decide : 3 ∣ 6) h6 have h2 : 2 ∣ n := dvd_trans (by decide : 2 ∣ 6) h6 have hlt43 : n/4 < n/3 := by have := (Nat.div_lt_div_left (a := n) (b := 4) (c := 3) hn0 h4 h3) simpa using this.2 (by decide : (3:ℕ) < 4) have hlt32 : n/3 < n/2 := by have := (Nat.div_lt_div_left (a := n) (b := 3) (c := 2) hn0 h3 h2) simpa using this.2 (by decide : (2:ℕ) < 3) have hne2 : n/4 ≠ n/2 := ne_of_lt (lt_trans hlt43 hlt32) have hne3 : n/4 ≠ n/3 := ne_of_lt hlt43 have hmem_pd : n/4 ∈ n.properDivisors := by refine (Nat.mem_properDivisors).2 ?_ constructor · exact Nat.div_dvd_of_dvd h4 · exact Nat.div_lt_self (Nat.pos_of_ne_zero hn0) (by decide : 1 < 4) exact (mem_erase_erase_iff (n := n) (b := n/4)).2 ⟨hmem_pd, hne2, hne3⟩ -- `max'_eq_iff` have hmax : ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' (⟨n/4, hmem⟩) = n/4 := by refine (Finset.max'_eq_iff (s := (n.properDivisors).erase (n/2) |>.erase (n/3)) (H := ⟨n/4, hmem⟩) (a := n/4)).2 ?_ constructor · exact hmem · intro b hb rcases (mem_erase_erase_iff (n := n) (b := b)).1 hb with ⟨hbpd, hbne2, hbne3⟩ by_contra hgt have hgt' : n/4 < b := lt_of_not_ge hgt -- get exact factorization `n = b*q` with `q>1` rcases (Nat.mem_properDivisors_iff_exists (m := b) (n := n) hn0).1 hbpd with ⟨q, hq, hn⟩ have hN : n < b * 4 := (Nat.div_lt_iff_lt_mul (by decide : 0 < 4)).1 hgt' have : b * q < b * 4 := by simpa [hn, Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hN have hq_lt4 : q < 4 := Nat.lt_of_mul_lt_mul_left this have hq_ge2 : 2 ≤ q := (Nat.succ_le_iff).2 hq have hq_cases : q = 2 ∨ q = 3 := by have hq_le3 : q ≤ 3 := Nat.lt_succ_iff.mp (by simpa using hq_lt4) omega rcases hq_cases with rfl | rfl · -- then `b = n/2` have : b = n/2 := by -- from `n = b*2` -- `Nat.mul_div_right` gives `(b*2)/2=b` simpa [hn, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc] using (Nat.mul_div_right b 2) exact hbne2 this · -- then `b = n/3` have : b = n/3 := by simpa [hn, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc] using (Nat.mul_div_right b 3) exact hbne3 this simpa [h4] using hmax · -- show `max'` equals `n/6` have hn0 : n ≠ 0 := Nat.ne_of_gt (lt_trans Nat.zero_lt_one hn1) have hmem : n/6 ∈ ((n.properDivisors).erase (n/2) |>.erase (n/3)) := by have h3 : 3 ∣ n := dvd_trans (by decide : 3 ∣ 6) h6 have h2 : 2 ∣ n := dvd_trans (by decide : 2 ∣ 6) h6 have hlt63 : n/6 < n/3 := by have := (Nat.div_lt_div_left (a := n) (b := 6) (c := 3) hn0 h6 h3) simpa using this.2 (by decide : (3:ℕ) < 6) have hlt32 : n/3 < n/2 := by have := (Nat.div_lt_div_left (a := n) (b := 3) (c := 2) hn0 h3 h2) simpa using this.2 (by decide : (2:ℕ) < 3) have hne2 : n/6 ≠ n/2 := ne_of_lt (lt_trans hlt63 hlt32) have hne3 : n/6 ≠ n/3 := ne_of_lt hlt63 have hmem_pd : n/6 ∈ n.properDivisors := by refine (Nat.mem_properDivisors).2 ?_ constructor · exact Nat.div_dvd_of_dvd h6 · exact Nat.div_lt_self (Nat.pos_of_ne_zero hn0) (by decide : 1 < 6) exact (mem_erase_erase_iff (n := n) (b := n/6)).2 ⟨hmem_pd, hne2, hne3⟩ have hmax : ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' (⟨n/6, hmem⟩) = n/6 := by refine (Finset.max'_eq_iff (s := (n.properDivisors).erase (n/2) |>.erase (n/3)) (H := ⟨n/6, hmem⟩) (a := n/6)).2 ?_ constructor · exact hmem · intro b hb rcases (mem_erase_erase_iff (n := n) (b := b)).1 hb with ⟨hbpd, hbne2, hbne3⟩ by_contra hgt have hgt' : n/6 < b := lt_of_not_ge hgt rcases (Nat.mem_properDivisors_iff_exists (m := b) (n := n) hn0).1 hbpd with ⟨q, hq, hn⟩ have hN : n < b * 6 := (Nat.div_lt_iff_lt_mul (by decide : 0 < 6)).1 hgt' have : b * q < b * 6 := by simpa [hn, Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hN have hq_lt6 : q < 6 := Nat.lt_of_mul_lt_mul_left this have hq_ge2 : 2 ≤ q := (Nat.succ_le_iff).2 hq have hq_cases : q = 2 ∨ q = 3 ∨ q = 4 ∨ q = 5 := by have hq_le5 : q ≤ 5 := Nat.lt_succ_iff.mp (by simpa using hq_lt6) omega rcases hq_cases with rfl | rfl | rfl | rfl · have : b = n/2 := by simpa [hn, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc] using (Nat.mul_div_right b 2) exact hbne2 this · have : b = n/3 := by simpa [hn, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc] using (Nat.mul_div_right b 3) exact hbne3 this · -- q=4 implies `4 ∣ n` have : 4 ∣ n := by refine ⟨b, by simpa [hn, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc]⟩ exact h4 this · -- q=5 implies `5 ∣ n` have : 5 ∣ n := by refine ⟨b, by simpa [hn, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc]⟩ exact h5 this simpa [h4] using hmax end Math.DivisorSequence