Lean / DevSingh05/oeis-a349787 / A349787.lean
Lean reader

A349787.lean

156 lines18 declarationsno sorryimports Mathlib
  1. import Mathlib
  2. /-!
  3. # A counterexample to Conjecture 1 of OEIS A349787
  4. Zhi-Wei Sun conjectured (OEIS A349787, 2021) that every integer `n ≥ 2` can be written as
  5. `n = x² + yᵏ + 2ᵃ + 2ᵇ` with nonnegative integers `x ≥ y`, `a ≥ b` and `k ∈ {2, 3}`.
  6. We show that `n = 60104967` has no such representation.
  7. The proof is a finite computation, checked by the Lean kernel (`decide +kernel`).
  8. * Since `2ᵃ ≤ n < 2²⁶`, there are at most 351 pairs `b ≤ a ≤ 25`. Put `m = n - 2ᵃ - 2ᵇ`
  9. (350 of the pairs give `m ≥ 0`).
  10. * `k = 3`: `y³ ≤ n` forces `y ≤ 391`, and for each such `y` we check that `m - y³` is not a
  11. square.
  12. * `k = 2`: a sum of two squares is never `3`, `6` or `7` modulo `8`, which disposes of all but
  13. 27 values of `m`. For those, `y ≤ x` forces `y ≤ 5482`, and for each such `y` we check that
  14. `m - y²` is not a square.
  15. * A number `r` is certified to be a non-square either by its residue modulo `64` or `63`, or by
  16. a number `t` with `(t - 1)² < r < (t + 1)²` and `r ≠ t²`; such a `t` is found by Newton's
  17. method, but the certificate does not depend on how `t` was obtained.
  18. The checking functions are written with `Nat.blt`, `Nat.beq` rather than `<`, `=`, because
  19. the kernel evaluates these (like `+`, `*`, `/`, `%`, `^`) directly on binary numerals.
  20. -/
  21. namespace A349787
  22. /-- `allBelow n p` decides whether `p i` holds for all `i < n`. -/
  23. def allBelow (n : ℕ) (p : ℕ → Bool) : Bool :=
  24. Nat.rec true (fun i ih => p i && ih) n
  25. /-- `allBelow n p` holds iff `p i` holds for all `i < n`. -/
  26. theorem allBelow_iff {n : ℕ} {p : ℕ → Bool} :
  27. allBelow n p = true ↔ ∀ i < n, p i = true := by
  28. induction n with
  29. | zero => simp [allBelow]
  30. | succ n ih =>
  31. rw [Nat.forall_lt_succ_right, ← ih]
  32. simp [allBelow, and_comm]
  33. /-- `nonResidue M S r` tests whether bit `r % M` of `S` is zero. It is used with `S` the number
  34. whose `i`-th bit is set iff `i` is a square modulo `M`, so that `r` is not a square modulo `M`. -/
  35. def nonResidue (M S r : ℕ) : Bool :=
  36. Nat.beq (S >>> (r % M) % 2) 0
  37. /-- The squares modulo `64`, as a bit mask. -/
  38. def squares64 : ℕ := 0x0202021202030213
  39. /-- The squares modulo `63`, as a bit mask. -/
  40. def squares63 : ℕ := 0x0402483012450293
  41. /-- A number close to `√r` (for `r < 7753²`), found by nine Newton steps starting from `7753`. -/
  42. def nearRoot (r : ℕ) : ℕ :=
  43. (fun s => (s + r / s) / 2)^[9] 7753
  44. /-- A sufficient test for `r` not being a perfect square. -/
  45. def nonSquare (r : ℕ) : Bool :=
  46. nonResidue 64 squares64 r || nonResidue 63 squares63 r ||
  47. let t := nearRoot r
  48. Nat.blt ((t - 1) ^ 2) r && !Nat.beq r (t ^ 2) && Nat.blt r ((t + 1) ^ 2)
  49. /-- If no square of a residue passes the test `nonResidue M S`, then no square does. -/
  50. theorem nonResidue_sq {M S : ℕ} (hM : 0 < M) (h : ∀ i < M, nonResidue M S (i ^ 2) = false)
  51. (x : ℕ) : nonResidue M S (x ^ 2) = false := by
  52. have e : (x % M) ^ 2 % M = x ^ 2 % M := (Nat.mod_modEq x M).pow 2
  53. rw [nonResidue, ← e]
  54. exact h _ (Nat.mod_lt _ hM)
  55. /-- If `nonSquare r` holds then `r` is not a perfect square. -/
  56. theorem nonSquare_sound {r : ℕ} (h : nonSquare r = true) (x : ℕ) : r ≠ x ^ 2 := by
  57. rintro rfl
  58. have h64 : nonResidue 64 squares64 (x ^ 2) = false :=
  59. nonResidue_sq (by norm_num) (by decide +kernel) x
  60. have h63 : nonResidue 63 squares63 (x ^ 2) = false :=
  61. nonResidue_sq (by norm_num) (by decide +kernel) x
  62. simp only [nonSquare, h64, h63, Bool.or_eq_true, Bool.false_eq_true, false_or,
  63. Bool.and_eq_true, Nat.blt_eq, Bool.not_eq_true'] at h
  64. obtain ⟨⟨h₁, h₂⟩, h₃⟩ := h
  65. have := lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) h₁
  66. have := lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) h₃
  67. have hx : x = nearRoot (x ^ 2) := by omega
  68. exact Nat.ne_of_beq_eq_false h₂ (congrArg (· ^ 2) hx)
  69. /-- `noRep k Y m` certifies that `m ≠ x² + yᵏ` for all `x` and all `y < Y`. -/
  70. def noRep (k Y m : ℕ) : Bool :=
  71. allBelow Y fun y => nonSquare (m - y ^ k) || Nat.blt m (y ^ k)
  72. /-- If `noRep k Y m` holds then `m ≠ x² + yᵏ` whenever `y < Y`. -/
  73. theorem noRep_sound {k Y m : ℕ} (h : noRep k Y m = true) {x y : ℕ} (hy : y < Y) :
  74. m ≠ x ^ 2 + y ^ k := by
  75. rintro rfl
  76. have := allBelow_iff.mp h y hy
  77. simp only [Nat.add_sub_cancel, Bool.or_eq_true, Nat.blt_eq] at this
  78. rcases this with h | h
  79. · exact nonSquare_sound h x rfl
  80. · omega
  81. /-- `notSumSqMod8 m` holds iff `m` is `3`, `6` or `7` modulo `8`. -/
  82. def notSumSqMod8 (m : ℕ) : Bool :=
  83. Nat.beq (m % 8) 3 || Nat.beq (m % 8) 6 || Nat.beq (m % 8) 7
  84. /-- A sum of two squares is not `3`, `6` or `7` modulo `8`. -/
  85. theorem notSumSqMod8_sound (x y : ℕ) : notSumSqMod8 (x ^ 2 + y ^ 2) = false := by
  86. have h : ∀ i < 8, ∀ j < 8, notSumSqMod8 (i ^ 2 + j ^ 2) = false := by decide
  87. have e : ((x % 8) ^ 2 + (y % 8) ^ 2) % 8 = (x ^ 2 + y ^ 2) % 8 :=
  88. ((Nat.mod_modEq x 8).pow 2).add ((Nat.mod_modEq y 8).pow 2)
  89. rw [notSumSqMod8, ← e]
  90. exact h _ (Nat.mod_lt _ (by norm_num)) _ (Nat.mod_lt _ (by norm_num))
  91. /-- `forPairs p` decides whether `p (n - 2ᵃ - 2ᵇ)` holds for all `b ≤ a ≤ 25` with
  92. `2ᵃ + 2ᵇ ≤ n`, where `n = 60104967`. -/
  93. def forPairs (p : ℕ → Bool) : Bool :=
  94. allBelow 26 fun a => allBelow (a + 1) fun b =>
  95. Nat.blt 60104967 (2 ^ a + 2 ^ b) || p (60104967 - 2 ^ a - 2 ^ b)
  96. /-- If `forPairs p` holds and `n = m + 2ᵃ + 2ᵇ` with `b ≤ a`, then `p m` holds. -/
  97. theorem forPairs_sound {p : ℕ → Bool} (h : forPairs p = true) {a b m : ℕ} (hba : b ≤ a)
  98. (hm : 60104967 = m + 2 ^ a + 2 ^ b) : p m = true := by
  99. have := Nat.two_pow_pos a
  100. have := Nat.two_pow_pos b
  101. have ha : a < 26 := (Nat.pow_lt_pow_iff_right (by norm_num : 1 < 2)).mp (by omega)
  102. have := allBelow_iff.mp (allBelow_iff.mp h a ha) b (by omega)
  103. have e : 60104967 - 2 ^ a - 2 ^ b = m := by omega
  104. simpa [e, show ¬ 60104967 < 2 ^ a + 2 ^ b by omega] using this
  105. /-- The case `k = 3`: no `m = n - 2ᵃ - 2ᵇ` is of the form `x² + y³` with `y ≤ 391`. -/
  106. theorem check_three : forPairs (noRep 3 392) = true := by
  107. decide +kernel
  108. /-- The case `k = 2`: no `m = n - 2ᵃ - 2ᵇ` is of the form `x² + y²` with `y ≤ 5482`. -/
  109. theorem check_two : forPairs (fun m => notSumSqMod8 m || noRep 2 5483 m) = true := by
  110. decide +kernel
  111. end A349787
  112. open A349787 in
  113. /-- **Counterexample to Conjecture 1 of OEIS A349787**: `60104967` is not of the form
  114. `x² + yᵏ + 2ᵃ + 2ᵇ` with `y ≤ x`, `b ≤ a` and `k ∈ {2, 3}`. -/
  115. theorem a349787_counterexample :
  116. ¬ ∃ x y a b k : ℕ, y ≤ x ∧ b ≤ a ∧ (k = 2 ∨ k = 3) ∧
  117. 60104967 = x ^ 2 + y ^ k + 2 ^ a + 2 ^ b := by
  118. rintro ⟨x, y, a, b, k, hyx, hba, hk, h⟩
  119. have := Nat.two_pow_pos a
  120. have := Nat.two_pow_pos b
  121. rcases hk with rfl | rfl
  122. · have hy : y ^ 2 < 5483 ^ 2 := by have := Nat.pow_le_pow_left hyx 2; omega
  123. have h2 := forPairs_sound check_two hba h
  124. rw [notSumSqMod8_sound, Bool.false_or] at h2
  125. exact noRep_sound h2 (lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) hy) rfl
  126. · have hy : y ^ 3 < 392 ^ 3 := by omega
  127. have h3 := forPairs_sound check_three hba h
  128. exact noRep_sound h3 (lt_of_pow_lt_pow_left₀ 3 (Nat.zero_le _) hy) rfl
  129. #print axioms a349787_counterexample