import Mathlib.NumberTheory.Divisors import Mathlib.Data.Finset.Max import Mathlib.Logic.Function.Iterate open Finset namespace DivisorSequence /-- The sum of the three largest proper divisors of `n`. If `n` has fewer than three proper divisors, we define this to be `0`. This is only used on inputs with at least three proper divisors. -/ noncomputable def next (n : ℕ) : ℕ := by classical let s : Finset ℕ := Nat.properDivisors n by_cases h3 : s.card ≥ 3 · -- take successive maxima 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 /-- A simplified transition function, valid for the divisor-sum dynamics on numbers divisible by `6` and not by `5`. -/ noncomputable def g (n : ℕ) : ℕ := if 4 ∣ n then 13 * (n / 12) else n /-- Candidate characterization of valid starting values, in a divisibility-friendly form. `Good n` means `n = 2^(2*k+1) * 3^ℓ * m` where `ℓ ≥ k+1` and `m` is coprime to `30`. This is equivalent (informally) to: * `6 ∣ n`, * `5 ∤ n`, * `v₂(n)` is odd, and * `v₃(n) ≥ (v₂(n)+1)/2`. -/ def Good (n : ℕ) : Prop := ∃ k ℓ m : ℕ, k + 1 ≤ ℓ ∧ Nat.Coprime m 30 ∧ n = 2^(2*k+1) * 3^ℓ * m /-- Iterating the simplified transition `g` decreases the `2`-adic exponent by `2` (and the `3`-adic exponent by `1`) until it reaches `v₂ = 1`. This lemma is proved only for numbers in the explicit normal form `Good`. -/ theorem g_iterate_on_good {n : ℕ} (hn : Good n) : ∃ k ℓ m : ℕ, k + 1 ≤ ℓ ∧ Nat.Coprime m 30 ∧ ((g^[k]) n = 13^k * 2 * 3^(ℓ-k) * m) ∧ ((g^[k+1]) n = (g^[k]) n) := by classical rcases hn with ⟨k, ℓ, m, hkl, hcop, rfl⟩ refine ⟨k, ℓ, m, hkl, hcop, ?_, ?_⟩ · induction k with | zero => simp [Nat.iterate, g] | succ k ih => -- TODO: complete (induction with algebra + divisibility) sorry · -- TODO: show `g` fixes the resulting number because it is not divisible by `4`. sorry /-- Key arithmetic lemma: on multiples of `6` not divisible by `5`, `next` agrees with the simplified function `g`. This is the hard divisor-ordering step. -/ theorem next_eq_g_of_mul6 {n : ℕ} (h6 : 6 ∣ n) (h5 : ¬ 5 ∣ n) : next n = g n := by sorry end DivisorSequence