import Mathlib.NumberTheory.Divisors import Mathlib.Data.Finset.Max import Mathlib.Data.Finset.Card import Mathlib.Tactic import Math.DivisorSequence.ProperDivisorsMax import Math.DivisorSequence.SecondLargest import Math.DivisorSequence.ThirdLargest2 open Finset namespace NextEqGOfMul6Proof2 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 noncomputable def g (n : ℕ) : ℕ := if 4 ∣ n then 13 * (n / 12) else 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 /-- Full proof of the arithmetic divisor-ordering lemma. This discharges the `next_eq_g_of_mul6` `sorry` in /[2jbmwt]/ using the published Lean lemmas /[w2gr4v]/, /[e4amdw]/, and /[0e9zti]/. -/ 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 := by exact le_trans (by decide : (3:ℕ) ≤ 3) (card_properDivisors_ge_three (n := n) h6 hn1) 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 := DivisorSequence.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 := Math.DivisorSequence.max'_erase_div_two_eq_div_three (n := n) h6 hn1 have hne1 : ((n.properDivisors).erase (n/2)).Nonempty := by refine ⟨n/3, Math.DivisorSequence.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, Math.DivisorSequence.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 := Math.DivisorSequence.max'_erase_div_two_erase_div_three_eq (n := n) h6 h5 hn1 -- use the same witness as in `ThirdLargest2` to avoid proof-irrelevance headaches let hne2 : ((n.properDivisors).erase (n/2) |>.erase (n/3)).Nonempty := by classical -- exactly the witness used by the theorem 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 (Math.DivisorSequence.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 (Math.DivisorSequence.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 -- now `hthird_std` has the same witness definition, so `simpa` works simpa [hne2] using hthird_std simp [hH2, hthird] -- now pure arithmetic by_cases h4 : 4 ∣ n · -- n/2 + n/3 + n/4 = 13*(n/12) have h3 : 3 ∣ n := dvd_trans (by decide : 3 ∣ 6) h6 have h34 : Nat.Coprime 3 4 := by decide have h12 : 12 ∣ n := by have : 3*4 ∣ n := h34.mul_dvd_of_dvd_of_dvd h3 h4 simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using this rcases h12 with ⟨k, rfl⟩ -- reduce to a concrete division identity on `12*k` have h2 : (12*k)/2 = 6*k := by calc (12*k)/2 = (2*(6*k))/2 := by ring_nf _ = 6*k := by exact Nat.mul_div_cancel_left (m := 6*k) (n := 2) (Nat.succ_pos 1) have h3' : (12*k)/3 = 4*k := by calc (12*k)/3 = (3*(4*k))/3 := by ring_nf _ = 4*k := by exact Nat.mul_div_cancel_left (m := 4*k) (n := 3) (Nat.succ_pos 2) have h4' : (12*k)/4 = 3*k := by calc (12*k)/4 = (4*(3*k))/4 := by ring_nf _ = 3*k := by exact Nat.mul_div_cancel_left (m := 3*k) (n := 4) (Nat.succ_pos 3) simp [h4] nth_rewrite 1 [h2] nth_rewrite 1 [h3'] nth_rewrite 1 [h4'] ring_nf · rcases h6 with ⟨k, rfl⟩ -- reduce to a concrete division identity on `6*k` have h2 : (6*k)/2 = 3*k := by calc (6*k)/2 = (2*(3*k))/2 := by ring_nf _ = 3*k := by exact Nat.mul_div_cancel_left (m := 3*k) (n := 2) (Nat.succ_pos 1) have h3' : (6*k)/3 = 2*k := by calc (6*k)/3 = (3*(2*k))/3 := by ring_nf _ = 2*k := by exact Nat.mul_div_cancel_left (m := 2*k) (n := 3) (Nat.succ_pos 2) simp [h4] nth_rewrite 1 [h2] nth_rewrite 1 [h3'] ring_nf