import Mathlib.NumberTheory.Divisors import Mathlib.Data.Finset.Max import Mathlib.Data.Finset.Card import Mathlib.Tactic open Finset namespace NextEqGOfMul6Standalone /-! Standalone Lean development proving the key lemma: `next n` (sum of 3 largest proper divisors via successive maxima) equals `g n := if 4 ∣ n then 13*(n/12) else n` under assumptions `6 ∣ n`, `¬ 5 ∣ n`, `1 < n`. This file is intentionally self-contained (imports only mathlib). -/ /-- For an even natural number `n>1`, the largest proper divisor is `n/2`. -/ theorem max'_properDivisors_eq_div_two {n : ℕ} (hn2 : 2 ∣ n) (hn1 : 1 < n) : (n.properDivisors).max' (by simpa [Nat.nonempty_properDivisors] using hn1) = n / 2 := by classical set H : (n.properDivisors).Nonempty := by simpa [Nat.nonempty_properDivisors] using hn1 refine (Finset.max'_eq_iff (s := n.properDivisors) (H := H) (a := n / 2)).2 ?_ constructor · -- show `n/2` is a proper divisor rcases hn2 with ⟨m, rfl⟩ have hmpos : 0 < m := by refine Nat.pos_of_ne_zero ?_ intro hm0 subst hm0 simpa using hn1 have hdiv : (2 * m) / 2 ∣ 2 * m := by simpa [Nat.mul_comm] using (dvd_mul_left m 2) have hlt : (2 * m) / 2 < 2 * m := by simpa using (Nat.mul_lt_mul_of_pos_right (by decide : (1:ℕ) < 2) hmpos) exact (Nat.mem_properDivisors).2 ⟨hdiv, hlt⟩ · intro b hb have hn0 : n ≠ 0 := Nat.ne_of_gt (lt_trans Nat.zero_lt_one hn1) rcases (Nat.mem_properDivisors_iff_exists (m := b) (n := n) hn0).1 hb with ⟨k, hk, hnk⟩ have hk2 : 2 ≤ k := (Nat.succ_le_iff).2 hk have hb2' : b * 2 ≤ b * k := Nat.mul_le_mul_left b hk2 have hb2 : b * 2 ≤ n := by simpa [hnk] using hb2' exact (Nat.le_div_iff_mul_le Nat.zero_lt_two).2 hb2 /-- Cofactor trick: a proper divisor strictly greater than `n/3` must be `n/2`. -/ theorem eq_div_two_of_mem_properDivisors_of_div_three_lt {n d : ℕ} (hd : d ∈ n.properDivisors) (h : n / 3 < d) : d = n / 2 := by classical have hn0 : n ≠ 0 := by intro hn0 subst hn0 simpa [Nat.properDivisors_zero] using hd -- write `n = d*k` with `k>1` rcases (Nat.mem_properDivisors_iff_exists (m := d) (n := n) hn0).1 hd with ⟨k, hk, hnk⟩ have hmul : n < d * 3 := (Nat.div_lt_iff_lt_mul (by decide : 0 < 3)).1 h have hmul' : d * k < d * 3 := by simpa [hnk, Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul have hklt3 : k < 3 := Nat.lt_of_mul_lt_mul_left hmul' have hk_le2 : k ≤ 2 := (Nat.lt_succ_iff).1 (by simpa using hklt3) have hk_ge2 : 2 ≤ k := (Nat.succ_le_iff).2 hk have hk2 : k = 2 := le_antisymm hk_le2 hk_ge2 have : n / 2 = d := by -- rewrite and simplify have : (d * 2) / 2 = d := by simpa using (Nat.mul_div_right d (by decide : 0 < (2:ℕ))) simpa [hnk, hk2, Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using this.symm simpa [this] /-- If `6 ∣ n` and `1 < n`, then `n/3` lies in `n.properDivisors.erase (n/2)`. -/ lemma div_three_mem_erase_div_two {n : ℕ} (h6 : 6 ∣ n) (hn1 : 1 < n) : n / 3 ∈ (n.properDivisors).erase (n / 2) := by classical 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 hlt : 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 hne : n / 3 ≠ n / 2 := ne_of_lt hlt refine Finset.mem_erase.2 ?_ refine ⟨hne, ?_⟩ refine (Nat.mem_properDivisors).2 ?_ constructor · exact Nat.div_dvd_of_dvd h3 · exact Nat.div_lt_self (Nat.pos_of_ne_zero hn0) (by decide : 1 < 3) /-- For `6 ∣ n` and `1 < n`, after erasing `n/2`, the maximum proper divisor is `n/3`. -/ theorem max'_erase_div_two_eq_div_three {n : ℕ} (h6 : 6 ∣ n) (hn1 : 1 < n) : ((n.properDivisors).erase (n / 2)).max' (by classical exact ⟨n/3, div_three_mem_erase_div_two (n := n) h6 hn1⟩ ) = n / 3 := by classical refine (Finset.max'_eq_iff (s := (n.properDivisors).erase (n/2)) (H := (by exact ⟨n/3, div_three_mem_erase_div_two (n := n) h6 hn1⟩)) (a := n/3)).2 ?_ constructor · exact div_three_mem_erase_div_two (n := n) h6 hn1 · intro b hb have hbpd : b ∈ n.properDivisors := (Finset.mem_erase.1 hb).2 have hbne : b ≠ n/2 := (Finset.mem_erase.1 hb).1 by_contra hgt have hgt' : n/3 < b := lt_of_not_ge hgt have : b = n/2 := eq_div_two_of_mem_properDivisors_of_div_three_lt (n := n) (d := b) hbpd hgt' exact hbne this /-- 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) := (Finset.mem_erase.2 ⟨hbne2, hbpd⟩) exact (Finset.mem_erase.2 ⟨hbne3, this⟩) /-- After erasing `n/2` and `n/3` from the proper divisors, the maximum is `n/4` if `4 ∣ n` and otherwise `n/6`, assuming `6 ∣ n` and `¬ 5 ∣ n` and `1 < n`. (Proof adapted from /[0e9zti]/.) -/ 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 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 · -- `n/4` is max have hn0 : n ≠ 0 := Nat.ne_of_gt (lt_trans Nat.zero_lt_one hn1) -- membership have hmem : n/4 ∈ ((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 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⟩ 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 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_cases : q = 2 ∨ q = 3 := by have hq_le3 : q ≤ 3 := Nat.lt_succ_iff.mp (by simpa using hq_lt4) have hq_ge2 : 2 ≤ q := (Nat.succ_le_iff).2 hq omega rcases hq_cases with 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 (by decide : 0 < (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 (by decide : 0 < (3:ℕ))) exact hbne3 this simpa [h4] using hmax · -- `n/6` is max 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_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) have hq_ge2 : 2 ≤ q := (Nat.succ_le_iff).2 hq 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 (by decide : 0 < (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 (by decide : 0 < (3:ℕ))) exact hbne3 this · have : 4 ∣ n := by refine ⟨b, by simpa [hn, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_assoc]⟩ exact h4 this · 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 /-- Arithmetic helper: the sum of the expected top three divisors. -/ def top3Sum (n : ℕ) : ℕ := n/2 + n/3 + (if 4 ∣ n then n/4 else n/6) /-- Closed form update used in the divisor-dynamics. -/ noncomputable def g (n : ℕ) : ℕ := if 4 ∣ n then 13 * (n / 12) else n /-- Pure arithmetic lemma: on multiples of `6`, `top3Sum = g`. Adapted from /[8i51qm]/. -/ theorem top3Sum_eq_g_of_mul6 {n : ℕ} (h6 : 6 ∣ n) : top3Sum n = g n := by classical by_cases h4 : 4 ∣ n · have h12 : 12 ∣ n := by have : Nat.lcm 4 6 ∣ n := Nat.lcm_dvd h4 h6 have hlcm : Nat.lcm 4 6 = 12 := by decide simpa [hlcm] using this rcases h12 with ⟨m, rfl⟩ have hdiv2 : (12 * m) / 2 = 6 * m := by calc (12 * m) / 2 = (2 * (6 * m)) / 2 := by ring_nf _ = 6 * m := by exact Nat.mul_div_cancel_left (m := 6 * m) (n := 2) (Nat.succ_pos 1) have hdiv3 : (12 * m) / 3 = 4 * m := by calc (12 * m) / 3 = (3 * (4 * m)) / 3 := by ring_nf _ = 4 * m := by exact Nat.mul_div_cancel_left (m := 4 * m) (n := 3) (Nat.succ_pos 2) have hdiv4 : (12 * m) / 4 = 3 * m := by calc (12 * m) / 4 = (4 * (3 * m)) / 4 := by ring_nf _ = 3 * m := by exact Nat.mul_div_cancel_left (m := 3 * m) (n := 4) (Nat.succ_pos 3) simp [top3Sum, g, h4, hdiv2, hdiv3, hdiv4] ring_nf · rcases h6 with ⟨m, rfl⟩ have hdiv2 : (6 * m) / 2 = 3 * m := by calc (6 * m) / 2 = (2 * (3 * m)) / 2 := by ring_nf _ = 3 * m := by exact Nat.mul_div_cancel_left (m := 3 * m) (n := 2) (Nat.succ_pos 1) have hdiv3 : (6 * m) / 3 = 2 * m := by calc (6 * m) / 3 = (3 * (2 * m)) / 3 := by ring_nf _ = 2 * m := by exact Nat.mul_div_cancel_left (m := 2 * m) (n := 3) (Nat.succ_pos 2) simp [top3Sum, g, h4, hdiv2, hdiv3] ring_nf /-- `next` defined as the sum of the three largest proper divisors by successive maxima. If `n` has fewer than three proper divisors, returns `0`. -/ noncomputable def next (n : ℕ) : ℕ := by classical let s : Finset ℕ := Nat.properDivisors n by_cases h3 : s.card ≥ 3 · let m1 : ℕ := s.max' (by have : 0 < s.card := lt_of_lt_of_le (by decide : (0:ℕ) < 3) h3 simpa [Finset.card_pos] using this) let s1 : Finset ℕ := s.erase m1 have hs1 : s1.card ≥ 2 := by have hm1 : m1 ∈ s := by simpa [m1] using s.max'_mem (by have : 0 < s.card := lt_of_lt_of_le (by decide : (0:ℕ) < 3) h3 simpa [Finset.card_pos] using this) simpa [s1, Finset.card_erase_of_mem hm1] using (Nat.sub_le_sub_right h3 1) let m2 : ℕ := s1.max' (by have : 0 < s1.card := lt_of_lt_of_le (by decide : (0:ℕ) < 2) hs1 simpa [Finset.card_pos] using this) let s2 : Finset ℕ := s1.erase m2 have hs2 : s2.Nonempty := by have hm2 : m2 ∈ s1 := by simpa [m2] using s1.max'_mem (by have : 0 < s1.card := lt_of_lt_of_le (by decide : (0:ℕ) < 2) hs1 simpa [Finset.card_pos] using this) have : s2.card ≥ 1 := by simpa [s2, Finset.card_erase_of_mem hm2] using (Nat.sub_le_sub_right hs1 1) have : 0 < s2.card := Nat.pos_of_ne_zero (by intro h have := this simp [h] at this) simpa [Finset.card_pos] using this let m3 : ℕ := s2.max' hs2 exact m1 + m2 + m3 · exact 0 /-- Lower bound on the number of proper divisors when `6 ∣ n` and `1 < n`. -/ lemma card_properDivisors_ge_three {n : ℕ} (h6 : 6 ∣ n) (hn1 : 1 < n) : 3 ≤ (n.properDivisors).card := by classical rcases h6 with ⟨k, rfl⟩ have hkpos : 0 < k := by have : 1 < 6 * k := hn1 nlinarith let s0 : Finset ℕ := {k, 2*k, 3*k} have hs0card : s0.card = 3 := by have hk2 : k ≠ 2*k := by nlinarith have hk3 : k ≠ 3*k := by nlinarith have h23 : 2*k ≠ 3*k := by nlinarith simp [s0, hk2, hk3, h23] have hs0sub : s0 ⊆ (Nat.properDivisors (6*k)) := by intro x hx have hx' : x = k ∨ x = 2*k ∨ x = 3*k := by simpa [s0] using hx rcases hx' with rfl | rfl | rfl · refine (Nat.mem_properDivisors).2 ?_ constructor · exact ⟨6, by ring⟩ · nlinarith · refine (Nat.mem_properDivisors).2 ?_ constructor · exact ⟨3, by ring⟩ · nlinarith · refine (Nat.mem_properDivisors).2 ?_ constructor · exact ⟨2, by ring⟩ · nlinarith have hcard := Finset.card_le_card hs0sub have : 3 ≤ (Nat.properDivisors (6*k)).card := by simpa [hs0card] using hcard simpa using this /-- **Main theorem**: on `6 ∣ n`, `¬ 5 ∣ n`, `1 < n`, we have `next n = g n`. This is the divisor-ordering lemma needed in the proof of the classification /[2jbmwt]/. -/ theorem next_eq_g_of_mul6 {n : ℕ} (h6 : 6 ∣ n) (h5 : ¬ 5 ∣ n) (hn1 : 1 < n) : next n = g n := by classical have hn2 : 2 ∣ n := dvd_trans (by decide : 2 ∣ 6) h6 have hcard : (n.properDivisors).card ≥ 3 := le_trans (by decide : (3:ℕ) ≤ 3) (card_properDivisors_ge_three (n := n) h6 hn1) -- unfold `next` simp (config := {zeta := true}) [next, g, hcard] -- 1st max have hne0 : (n.properDivisors).Nonempty := by simpa [Nat.nonempty_properDivisors] using hn1 have hmax0 : (n.properDivisors).max' hne0 = n/2 := max'_properDivisors_eq_div_two (n := n) hn2 hn1 have hH0 : (n.properDivisors).max' (by have : 0 < (n.properDivisors).card := lt_of_lt_of_le (by decide : (0:ℕ) < 3) hcard simpa [Finset.card_pos] using this) = (n.properDivisors).max' hne0 := by classical cases Subsingleton.elim (by have : 0 < (n.properDivisors).card := lt_of_lt_of_le (by decide : (0:ℕ) < 3) hcard simpa [Finset.card_pos] using this : (n.properDivisors).Nonempty) hne0 rfl simp [hH0, hmax0] -- 2nd max have hsec_std := max'_erase_div_two_eq_div_three (n := n) h6 hn1 have hne1 : ((n.properDivisors).erase (n/2)).Nonempty := by refine ⟨n/3, div_three_mem_erase_div_two (n := n) h6 hn1⟩ have hH1 : ((n.properDivisors).erase (n/2)).max' (by have : 0 < ((n.properDivisors).erase (n/2)).card := Finset.card_pos.mpr hne1 simpa [Finset.card_pos] using this) = ((n.properDivisors).erase (n/2)).max' hne1 := by classical cases Subsingleton.elim (by have : 0 < ((n.properDivisors).erase (n/2)).card := Finset.card_pos.mpr hne1 simpa [Finset.card_pos] using this : ((n.properDivisors).erase (n/2)).Nonempty) hne1 rfl have hsec : ((n.properDivisors).erase (n/2)).max' hne1 = n/3 := by have hne1std : ((n.properDivisors).erase (n/2)).Nonempty := ⟨n/3, div_three_mem_erase_div_two (n := n) h6 hn1⟩ have hwit : ((n.properDivisors).erase (n/2)).max' hne1 = ((n.properDivisors).erase (n/2)).max' hne1std := by classical cases Subsingleton.elim hne1 hne1std rfl simpa [hwit] using hsec_std simp [hH1, hsec] -- 3rd max have hthird_std := max'_erase_div_two_erase_div_three_eq (n := n) h6 h5 hn1 -- use the same witness as in `max'_erase_div_two_erase_div_three_eq` let hne2 : ((n.properDivisors).erase (n/2) |>.erase (n/3)).Nonempty := 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 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 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) 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 hlt62 : n/6 < n/2 := by have := (Nat.div_lt_div_left (a := n) (b := 6) (c := 2) hn0 h6 h2) simpa using this.2 (by decide : (2:ℕ) < 6) have hne2' : n/6 ≠ n/2 := ne_of_lt hlt62 have hne3' : n/6 ≠ n/3 := ne_of_lt hlt63 exact (mem_erase_erase_iff (n := n) (b := n/6)).2 ⟨hmem_pd, hne2', hne3'⟩ have hH2 : ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' (by have : 0 < (((n.properDivisors).erase (n/2) |>.erase (n/3)).card) := Finset.card_pos.mpr hne2 simpa [Finset.card_pos] using this) = ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' hne2 := by classical cases Subsingleton.elim (by have : 0 < (((n.properDivisors).erase (n/2) |>.erase (n/3)).card) := Finset.card_pos.mpr hne2 simpa [Finset.card_pos] using this : ((n.properDivisors).erase (n/2) |>.erase (n/3)).Nonempty) hne2 rfl have hthird : ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' hne2 = (if 4 ∣ n then n/4 else n/6) := by -- reconcile witnesses have hwit : ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' hne2 = ((n.properDivisors).erase (n/2) |>.erase (n/3)).max' (by classical -- the witness created inside `hthird_std` have : (((n.properDivisors).erase (n/2) |>.erase (n/3))).Nonempty := by classical -- use the same definition as `hne2` exact hne2 exact this) := by classical -- proof irrelevance cases Subsingleton.elim (hne2) (by classical exact hne2) rfl -- use `hthird_std` simpa [hwit] using hthird_std simp [hH2, hthird] -- arithmetic finish simpa [top3Sum] using (top3Sum_eq_g_of_mul6 (n := n) h6) end NextEqGOfMul6Standalone