import Mathlib/-!# A counterexample to Conjecture 1 of OEIS A349787Zhi-Wei Sun conjectured (OEIS A349787, 2021) that every integer `n ≥ 2` can be written as`n = x² + yᵏ + 2ᵃ + 2ᵇ` with nonnegative integers `x ≥ y`, `a ≥ b` and `k ∈ {2, 3}`.We show that `n = 60104967` has no such representation.The proof is a finite computation, checked by the Lean kernel (`decide +kernel`).* Since `2ᵃ ≤ n < 2²⁶`, there are at most 351 pairs `b ≤ a ≤ 25`. Put `m = n - 2ᵃ - 2ᵇ`(350 of the pairs give `m ≥ 0`).* `k = 3`: `y³ ≤ n` forces `y ≤ 391`, and for each such `y` we check that `m - y³` is not asquare.* `k = 2`: a sum of two squares is never `3`, `6` or `7` modulo `8`, which disposes of all but27 values of `m`. For those, `y ≤ x` forces `y ≤ 5482`, and for each such `y` we check that`m - y²` is not a square.* A number `r` is certified to be a non-square either by its residue modulo `64` or `63`, or bya number `t` with `(t - 1)² < r < (t + 1)²` and `r ≠ t²`; such a `t` is found by Newton'smethod, but the certificate does not depend on how `t` was obtained.The checking functions are written with `Nat.blt`, `Nat.beq` rather than `<`, `=`, becausethe kernel evaluates these (like `+`, `*`, `/`, `%`, `^`) directly on binary numerals.-/namespace A349787/-- `allBelow n p` decides whether `p i` holds for all `i < n`. -/def allBelow (n : ℕ) (p : ℕ → Bool) : Bool :=Nat.rec true (fun i ih => p i && ih) n/-- `allBelow n p` holds iff `p i` holds for all `i < n`. -/theorem allBelow_iff {n : ℕ} {p : ℕ → Bool} :allBelow n p = true ↔ ∀ i < n, p i = true := byinduction n with| zero => simp [allBelow]| succ n ih =>rw [Nat.forall_lt_succ_right, ← ih]simp [allBelow, and_comm]/-- `nonResidue M S r` tests whether bit `r % M` of `S` is zero. It is used with `S` the numberwhose `i`-th bit is set iff `i` is a square modulo `M`, so that `r` is not a square modulo `M`. -/def nonResidue (M S r : ℕ) : Bool :=Nat.beq (S >>> (r % M) % 2) 0/-- The squares modulo `64`, as a bit mask. -/def squares64 : ℕ := 0x0202021202030213/-- The squares modulo `63`, as a bit mask. -/def squares63 : ℕ := 0x0402483012450293/-- A number close to `√r` (for `r < 7753²`), found by nine Newton steps starting from `7753`. -/def nearRoot (r : ℕ) : ℕ :=(fun s => (s + r / s) / 2)^[9] 7753/-- A sufficient test for `r` not being a perfect square. -/def nonSquare (r : ℕ) : Bool :=nonResidue 64 squares64 r || nonResidue 63 squares63 r ||let t := nearRoot rNat.blt ((t - 1) ^ 2) r && !Nat.beq r (t ^ 2) && Nat.blt r ((t + 1) ^ 2)/-- If no square of a residue passes the test `nonResidue M S`, then no square does. -/theorem nonResidue_sq {M S : ℕ} (hM : 0 < M) (h : ∀ i < M, nonResidue M S (i ^ 2) = false)(x : ℕ) : nonResidue M S (x ^ 2) = false := byhave e : (x % M) ^ 2 % M = x ^ 2 % M := (Nat.mod_modEq x M).pow 2rw [nonResidue, ← e]exact h _ (Nat.mod_lt _ hM)/-- If `nonSquare r` holds then `r` is not a perfect square. -/theorem nonSquare_sound {r : ℕ} (h : nonSquare r = true) (x : ℕ) : r ≠ x ^ 2 := byrintro rflhave h64 : nonResidue 64 squares64 (x ^ 2) = false :=nonResidue_sq (by norm_num) (by decide +kernel) xhave h63 : nonResidue 63 squares63 (x ^ 2) = false :=nonResidue_sq (by norm_num) (by decide +kernel) xsimp only [nonSquare, h64, h63, Bool.or_eq_true, Bool.false_eq_true, false_or,Bool.and_eq_true, Nat.blt_eq, Bool.not_eq_true'] at hobtain ⟨⟨h₁, h₂⟩, h₃⟩ := hhave := lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) h₁have := lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) h₃have hx : x = nearRoot (x ^ 2) := by omegaexact Nat.ne_of_beq_eq_false h₂ (congrArg (· ^ 2) hx)/-- `noRep k Y m` certifies that `m ≠ x² + yᵏ` for all `x` and all `y < Y`. -/def noRep (k Y m : ℕ) : Bool :=allBelow Y fun y => nonSquare (m - y ^ k) || Nat.blt m (y ^ k)/-- If `noRep k Y m` holds then `m ≠ x² + yᵏ` whenever `y < Y`. -/theorem noRep_sound {k Y m : ℕ} (h : noRep k Y m = true) {x y : ℕ} (hy : y < Y) :m ≠ x ^ 2 + y ^ k := byrintro rflhave := allBelow_iff.mp h y hysimp only [Nat.add_sub_cancel, Bool.or_eq_true, Nat.blt_eq] at thisrcases this with h | h· exact nonSquare_sound h x rfl· omega/-- `notSumSqMod8 m` holds iff `m` is `3`, `6` or `7` modulo `8`. -/def notSumSqMod8 (m : ℕ) : Bool :=Nat.beq (m % 8) 3 || Nat.beq (m % 8) 6 || Nat.beq (m % 8) 7/-- A sum of two squares is not `3`, `6` or `7` modulo `8`. -/theorem notSumSqMod8_sound (x y : ℕ) : notSumSqMod8 (x ^ 2 + y ^ 2) = false := byhave h : ∀ i < 8, ∀ j < 8, notSumSqMod8 (i ^ 2 + j ^ 2) = false := by decidehave e : ((x % 8) ^ 2 + (y % 8) ^ 2) % 8 = (x ^ 2 + y ^ 2) % 8 :=((Nat.mod_modEq x 8).pow 2).add ((Nat.mod_modEq y 8).pow 2)rw [notSumSqMod8, ← e]exact h _ (Nat.mod_lt _ (by norm_num)) _ (Nat.mod_lt _ (by norm_num))/-- `forPairs p` decides whether `p (n - 2ᵃ - 2ᵇ)` holds for all `b ≤ a ≤ 25` with`2ᵃ + 2ᵇ ≤ n`, where `n = 60104967`. -/def forPairs (p : ℕ → Bool) : Bool :=allBelow 26 fun a => allBelow (a + 1) fun b =>Nat.blt 60104967 (2 ^ a + 2 ^ b) || p (60104967 - 2 ^ a - 2 ^ b)/-- If `forPairs p` holds and `n = m + 2ᵃ + 2ᵇ` with `b ≤ a`, then `p m` holds. -/theorem forPairs_sound {p : ℕ → Bool} (h : forPairs p = true) {a b m : ℕ} (hba : b ≤ a)(hm : 60104967 = m + 2 ^ a + 2 ^ b) : p m = true := byhave := Nat.two_pow_pos ahave := Nat.two_pow_pos bhave ha : a < 26 := (Nat.pow_lt_pow_iff_right (by norm_num : 1 < 2)).mp (by omega)have := allBelow_iff.mp (allBelow_iff.mp h a ha) b (by omega)have e : 60104967 - 2 ^ a - 2 ^ b = m := by omegasimpa [e, show ¬ 60104967 < 2 ^ a + 2 ^ b by omega] using this/-- The case `k = 3`: no `m = n - 2ᵃ - 2ᵇ` is of the form `x² + y³` with `y ≤ 391`. -/theorem check_three : forPairs (noRep 3 392) = true := bydecide +kernel/-- The case `k = 2`: no `m = n - 2ᵃ - 2ᵇ` is of the form `x² + y²` with `y ≤ 5482`. -/theorem check_two : forPairs (fun m => notSumSqMod8 m || noRep 2 5483 m) = true := bydecide +kernelend A349787open A349787 in/-- **Counterexample to Conjecture 1 of OEIS A349787**: `60104967` is not of the form`x² + yᵏ + 2ᵃ + 2ᵇ` with `y ≤ x`, `b ≤ a` and `k ∈ {2, 3}`. -/theorem a349787_counterexample :¬ ∃ x y a b k : ℕ, y ≤ x ∧ b ≤ a ∧ (k = 2 ∨ k = 3) ∧60104967 = x ^ 2 + y ^ k + 2 ^ a + 2 ^ b := byrintro ⟨x, y, a, b, k, hyx, hba, hk, h⟩have := Nat.two_pow_pos ahave := Nat.two_pow_pos brcases hk with rfl | rfl· have hy : y ^ 2 < 5483 ^ 2 := by have := Nat.pow_le_pow_left hyx 2; omegahave h2 := forPairs_sound check_two hba hrw [notSumSqMod8_sound, Bool.false_or] at h2exact noRep_sound h2 (lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) hy) rfl· have hy : y ^ 3 < 392 ^ 3 := by omegahave h3 := forPairs_sound check_three hba hexact noRep_sound h3 (lt_of_pow_lt_pow_left₀ 3 (Nat.zero_le _) hy) rfl#print axioms a349787_counterexample