import Mathlib.Tactic /-- Bonza property, stated in `ℤ` to avoid subtraction issues in `ℕ`. -/ def Bonza (f : ℕ → ℕ) : Prop := (∀ a b : ℕ, 0 < a → 0 < b → (f a : ℤ) ∣ (b : ℤ)^a - (f b : ℤ)^(f a)) ∧ (∀ n : ℕ, 0 < n → 0 < f n) /-- An explicit candidate achieving ratio `4` at `n=4`. -/ def g (n : ℕ) : ℕ := if n = 1 then 1 else if n = 2 then 4 else if n = 4 then 16 else if n % 2 = 1 then 1 else 2 namespace BonzaExample2 lemma g_one : g 1 = 1 := by simp [g] lemma g_two : g 2 = 4 := by simp [g] lemma g_four : g 4 = 16 := by simp [g] lemma g_pos (n : ℕ) (hn : 0 < n) : 0 < g n := by unfold g by_cases h1 : n = 1 · simp [h1] by_cases h2 : n = 2 · simp [h1, h2] by_cases h4 : n = 4 · simp [h1, h2, h4] by_cases ho : n % 2 = 1 · simp [h1, h2, h4, ho] · simp [h1, h2, h4, ho] lemma g_eq_one_of_odd {n : ℕ} (hn : n % 2 = 1) : g n = 1 := by by_cases h1 : n = 1 · simp [g, h1] have h2 : n ≠ 2 := by intro h subst h simpa using hn have h4 : n ≠ 4 := by intro h subst h simpa using hn simp [g, h1, h2, h4, hn] lemma g_eq_two_of_even {n : ℕ} (hn : n % 2 = 0) (h1 : n ≠ 1) (h2 : n ≠ 2) (h4 : n ≠ 4) : g n = 2 := by simp [g, h1, h2, h4, hn] /-- If `b` is odd then `4 ∣ b^2 - 1` in `ℤ`. -/ lemma four_dvd_sq_sub_one_of_odd {b : ℕ} (hb : b % 2 = 1) : (4 : ℤ) ∣ (b : ℤ)^2 - 1 := by have hbOdd : Odd b := (Nat.odd_iff).2 hb rcases hbOdd with ⟨k, rfl⟩ have hbZ : ((2*k+1 : ℕ) : ℤ) = (2 : ℤ) * (k : ℤ) + 1 := by norm_cast refine ⟨(k : ℤ) * ((k : ℤ) + 1), ?_⟩ simp [hbZ] ring_nf /-- If `b` is even then `4 ∣ b^2` in `ℤ`. -/ lemma four_dvd_sq_of_even {b : ℕ} (hb : b % 2 = 0) : (4 : ℤ) ∣ (b : ℤ)^2 := by have hbEven : Even b := (Nat.even_iff).2 hb rcases hbEven with ⟨k, rfl⟩ have hbZ : ((k + k : ℕ) : ℤ) = (2 : ℤ) * (k : ℤ) := by norm_cast ring refine ⟨(k : ℤ)^2, ?_⟩ simp [hbZ] ring_nf /-- If `b` is odd then `16 ∣ b^4 - 1` in `ℤ`. -/ lemma sixteen_dvd_fourth_sub_one_of_odd {b : ℕ} (hb : b % 2 = 1) : (16 : ℤ) ∣ (b : ℤ)^4 - 1 := by have hbOdd : Odd b := (Nat.odd_iff).2 hb rcases hbOdd with ⟨k, rfl⟩ -- Let `s = k*(k+1)`; then `(2k+1)^4-1 = 8*s*(2*s+1)` and `s` is even. set s : ℤ := (k : ℤ) * ((k : ℤ) + 1) have hsEvenNat : Even (k * (k + 1)) := by rcases Nat.even_or_odd k with hk | hk · -- k = 2t rcases hk with ⟨t, rfl⟩ refine ⟨t * (t + t + 1), by ring⟩ · -- k = 2t+1 rcases hk with ⟨t, rfl⟩ refine ⟨(t + 1) * (t + t + 1), by ring⟩ -- convert to `ℤ`: `s = u+u` have hsEven : Even s := by rcases hsEvenNat with ⟨u, hu⟩ refine ⟨(u : ℤ), ?_⟩ dsimp [s] -- cast `hu` and finish have : (k : ℤ) * ((k : ℤ) + 1) = (u : ℤ) + u := by exact_mod_cast hu simpa using this rcases hsEven with ⟨u, hu⟩ have hfac : (( (2*k+1 : ℕ) : ℤ)^4 - 1) = 8*s*(2*s+1) := by simp [s] ring_nf refine ⟨u * (4*u + 1), ?_⟩ calc (( (2*k+1 : ℕ) : ℤ)^4 - 1) = 8*s*(2*s+1) := hfac _ = 8*(2*u)*(2*(2*u)+1) := by simp [hu, two_mul] _ = 16*(u*(4*u+1)) := by ring_nf /-- If `b` is even then `16 ∣ b^4` in `ℤ`. -/ lemma sixteen_dvd_fourth_of_even {b : ℕ} (hb : b % 2 = 0) : (16 : ℤ) ∣ (b : ℤ)^4 := by have hbEven : Even b := (Nat.even_iff).2 hb rcases hbEven with ⟨k, rfl⟩ have hbZ : ((k + k : ℕ) : ℤ) = (2 : ℤ) * (k : ℤ) := by norm_cast ring refine ⟨(k : ℤ)^4, ?_⟩ simp [hbZ, mul_pow] /-- Parity lemma: for any `a>0`, `2 ∣ b^a - g(b)^2` in `ℤ`. -/ lemma two_dvd_pow_sub_gsq (a b : ℕ) (ha : 0 < a) : (2 : ℤ) ∣ (b : ℤ)^a - (g b : ℤ)^2 := by have ha0 : a ≠ 0 := Nat.ne_of_gt ha rcases Nat.mod_two_eq_zero_or_one b with hb0 | hb1 · -- b even have hb2 : (2 : ℤ) ∣ (b : ℤ) := by have hbEven : Even b := (Nat.even_iff).2 hb0 rcases hbEven with ⟨k, rfl⟩ refine ⟨(k : ℤ), by norm_cast; ring⟩ have hbpow : (2 : ℤ) ∣ (b : ℤ)^a := dvd_pow hb2 ha0 have hgbpow : (2 : ℤ) ∣ (g b : ℤ)^2 := by by_cases h2 : b = 2 · subst h2 change (2 : ℤ) ∣ (4 : ℤ)^2 decide by_cases h4 : b = 4 · subst h4 change (2 : ℤ) ∣ (16 : ℤ)^2 decide have h1 : b ≠ 1 := by intro h; subst h; simpa using hb0 have gb : g b = 2 := g_eq_two_of_even (n := b) hb0 h1 h2 h4 have h : (2 : ℤ) ∣ (4 : ℤ) := by decide simpa [gb] using h exact dvd_sub hbpow hgbpow · -- b odd have gb : g b = 1 := g_eq_one_of_odd (n := b) hb1 have hbOdd : Odd (b : ℤ) := by rcases (Nat.odd_iff).2 hb1 with ⟨k, rfl⟩ refine ⟨(k : ℤ), by norm_cast⟩ have hbOddPow : Odd ((b : ℤ)^a) := Odd.pow (n := a) hbOdd rcases hbOddPow with ⟨t, ht⟩ have : (2 : ℤ) ∣ (b : ℤ)^a - 1 := by refine ⟨t, by linarith [ht]⟩ simpa [gb] using this /-- `g` is bonza. -/ theorem bonza_g : Bonza g := by refine ⟨?_, ?_⟩ · intro a b ha hb by_cases ha1 : a = 1 · subst ha1 simp [g] by_cases ha2 : a = 2 · subst ha2 -- goal: 4 ∣ b^2 - (g b)^4 change (4 : ℤ) ∣ (b : ℤ)^2 - (g b : ℤ)^4 rcases Nat.mod_two_eq_zero_or_one b with hb0 | hb1 · -- b even by_cases hb2 : b = 2 · subst hb2; simp [g] by_cases hb4 : b = 4 · subst hb4; simp [g] have h1 : b ≠ 1 := by intro h subst h simpa using hb0 have gb : g b = 2 := g_eq_two_of_even (n := b) hb0 h1 hb2 hb4 have h4b : (4 : ℤ) ∣ (b : ℤ)^2 := four_dvd_sq_of_even (b := b) hb0 have h416 : (4 : ℤ) ∣ (16 : ℤ) := by decide have : (4 : ℤ) ∣ (b : ℤ)^2 - (16 : ℤ) := dvd_sub h4b h416 simpa [gb] using this · -- b odd have gb : g b = 1 := g_eq_one_of_odd (n := b) hb1 have : (4 : ℤ) ∣ (b : ℤ)^2 - 1 := four_dvd_sq_sub_one_of_odd (b := b) hb1 simpa [gb] using this by_cases ha4 : a = 4 · subst ha4 -- goal: 16 ∣ b^4 - (g b)^16 change (16 : ℤ) ∣ (b : ℤ)^4 - (g b : ℤ)^16 rcases Nat.mod_two_eq_zero_or_one b with hb0 | hb1 · -- b even by_cases hb2 : b = 2 · subst hb2; simp [g] by_cases hb4 : b = 4 · subst hb4; simp [g] have h1 : b ≠ 1 := by intro h subst h simpa using hb0 have gb : g b = 2 := g_eq_two_of_even (n := b) hb0 h1 hb2 hb4 have h16b : (16 : ℤ) ∣ (b : ℤ)^4 := sixteen_dvd_fourth_of_even (b := b) hb0 have h16pow : (16 : ℤ) ∣ (2 : ℤ)^16 := by decide have : (16 : ℤ) ∣ (b : ℤ)^4 - (2 : ℤ)^16 := dvd_sub h16b h16pow simpa [gb] using this · -- b odd have gb : g b = 1 := g_eq_one_of_odd (n := b) hb1 have : (16 : ℤ) ∣ (b : ℤ)^4 - 1 := sixteen_dvd_fourth_sub_one_of_odd (b := b) hb1 simpa [gb] using this · -- remaining a: g a = 1 if a odd, else 2 rcases Nat.mod_two_eq_zero_or_one a with ha0 | ha1mod · -- a even => g a = 2 have h1 : a ≠ 1 := ha1 have h2 : a ≠ 2 := ha2 have h4 : a ≠ 4 := ha4 have ga : g a = 2 := g_eq_two_of_even (n := a) ha0 h1 h2 h4 -- need 2 ∣ b^a - g(b)^2 have : (2 : ℤ) ∣ (b : ℤ)^a - (g b : ℤ)^2 := two_dvd_pow_sub_gsq a b ha simpa [ga, pow_succ] using this · -- a odd => g a = 1 have ga : g a = 1 := g_eq_one_of_odd (n := a) ha1mod simpa [ga] · intro n hn exact g_pos n hn end BonzaExample2