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 BonzaExample 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_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_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 [Nat.mod_two_eq_one] 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⟩ refine ⟨(k : ℤ) * ((k : ℤ) + 1), by ring⟩ /-- 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⟩ refine ⟨(k : ℤ)^2, by ring⟩ /-- 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⟩ -- Reduce to showing an extra factor 2 in `k*(k+1)`. have h2 : (2 : ℤ) ∣ (k : ℤ) * ((k : ℤ) + 1) := by -- among `k` and `k+1` one is even rcases Nat.even_or_odd k with hk | hk · rcases hk with ⟨t, rfl⟩ refine ⟨(t : ℤ) * ((t + t : ℤ) + 1), by ring⟩ · rcases hk with ⟨t, rfl⟩ -- k = 2t+1 refine ⟨((t : ℤ) + 1) * ((2 * t + 1 : ℤ)), by ring⟩ rcases h2 with ⟨u, hu⟩ -- Let `s = k*(k+1)`. Then `(2k+1)^4-1 = 8*s*(2*s+1)`. have hfac : (( (2*k+1 : ℕ) : ℤ)^4 - 1) = 8 * ((k : ℤ) * ((k : ℤ) + 1)) * (2 * ((k : ℤ) * ((k : ℤ) + 1)) + 1) := by ring -- Substitute `((k:ℤ)*(k+1)) = 2*u`. refine ⟨u * (4*u + 1), ?_⟩ calc (( (2*k+1 : ℕ) : ℤ)^4 - 1) = 8 * ((k : ℤ) * ((k : ℤ) + 1)) * (2 * ((k : ℤ) * ((k : ℤ) + 1)) + 1) := hfac _ = 8 * (2*u) * (2*(2*u) + 1) := by simp [hu] _ = 16 * (u * (4*u + 1)) := by ring /-- 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⟩ refine ⟨(k : ℤ)^4, by ring⟩ /-- A parity lemma for the example: `2 ∣ b^a - g(b)^2` for all `a>0`. -/ lemma two_dvd_pow_sub_gsq (a b : ℕ) (ha : 0 < a) : (2 : ℤ) ∣ (b : ℤ)^a - (g b : ℤ)^2 := by -- use the fact that the cast to `ZMod 2` is zero have ha0 : a ≠ 0 := Nat.ne_of_gt ha have hz : (( (b : ℤ)^a - (g b : ℤ)^2) : ZMod 2) = 0 := by rcases Nat.mod_two_eq_zero_or_one b with hb0 | hb1 · -- b even have hbZ : ((b : ℕ) : ZMod 2) = (0 : ZMod 2) := by -- `b % 2 = 0` in this branch have hbmod : b % 2 = 0 := hb0 -- use `natCast_mod` have := (ZMod.natCast_mod b 2).symm -- `simp` reduces `(b % 2 : ZMod 2)` simpa [hbmod] using this have hgZ : ((g b : ℕ) : ZMod 2) = 0 := by -- if b is even then g b is even (2,4,16) by_cases h2 : b = 2 · subst h2; simp [g] by_cases h4 : b = 4 · subst h4; simp [g] by_cases h1 : b = 1 · subst h1; simp at hb0 -- remaining even => g b = 2 have : g b = 2 := by -- b is not 1,2,4 and b%2=0 simp [g, h1, h2, h4, hb0] simp [this] -- now compute in `ZMod 2` simp [hbZ, hgZ, ha0] · -- b odd have hbZ : ((b : ℕ) : ZMod 2) = 1 := by have hbmod : b % 2 = 1 := hb1 have := (ZMod.natCast_mod b 2).symm simpa [hbmod] using this have hgZ : ((g b : ℕ) : ZMod 2) = 1 := by -- if b is odd then g b = 1 have : g b = 1 := g_eq_one_of_odd (b := b) hb1 simp [this] simp [hbZ, hgZ, ha0] -- convert back to divisibility in `ℤ` exact (ZMod.intCast_zmod_eq_zero_iff_dvd (a := (b : ℤ)^a - (g b : ℤ)^2) (b := 2)).1 hz /-- `g` is bonza. -/ theorem bonza_g : Bonza g := by refine ⟨?_, ?_⟩ · intro a b ha hb -- casework on `a` (hence on `g a`) by_cases ha1 : a = 1 · subst ha1 simp [g] by_cases ha2 : a = 2 · subst ha2 -- need `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 gb : g b = 2 := by have hb1ne : b ≠ 1 := by intro h; subst h; simpa using hb0 exact g_eq_two_of_even (n := b) hb0 hb1ne hb2 hb4 -- show 4 divides b^2 - 16 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 [g, ha1, 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, g, ha1] using this by_cases ha4 : a = 4 · subst ha4 -- need `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 gb : g b = 2 := by have hb1ne : b ≠ 1 := by intro h; subst h; simpa using hb0 exact g_eq_two_of_even (n := b) hb0 hb1ne hb2 hb4 have h16b : (16 : ℤ) ∣ (b : ℤ)^4 := sixteen_dvd_fourth_of_even (b := b) hb0 have h16pow : (16 : ℤ) ∣ (2 : ℤ)^16 := by -- trivial since 16 divides 2^16 decide have : (16 : ℤ) ∣ (b : ℤ)^4 - (2 : ℤ)^16 := dvd_sub h16b h16pow simpa [g, ha1, ha2, 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 [g, ha1, ha2, gb] using this · -- remaining `a`: then `g a` is `1` if `a` is odd, otherwise `2`. rcases Nat.mod_two_eq_zero_or_one a with ha0 | ha1mod · -- a even have ga : g a = 2 := by -- since a ≠ 1,2,4 and a%2=0 have ha1ne : a ≠ 1 := ha1 have ha2ne : a ≠ 2 := ha2 have ha4ne : a ≠ 4 := ha4 exact g_eq_two_of_even (n := a) ha0 ha1ne ha2ne ha4ne -- reduce to the lemma `two_dvd_pow_sub_gsq` have : (2 : ℤ) ∣ (b : ℤ)^a - (g b : ℤ)^2 := two_dvd_pow_sub_gsq a b ha simpa [ga] using this · -- a odd have ga : g a = 1 := g_eq_one_of_odd (n := a) ha1mod simpa [ga] · intro n hn exact g_pos n hn end BonzaExample