import Mathlib/-!# Round 3: the residue filter of the search, the Mordell reduction, and the square conditions of D81. **Soundness of the residue filter** (`filter_sound`, `pow_mod_mem_image`). The search program `o7_wheel.c`discards a pair `(P, Q)` when `sp·P^ep + sq·Q^eq` is not an `es`-th power modulo some `m`, where the table of`es`-th powers mod `m` is built as `{t^es mod m : 0 ≤ t < m}`. If `S^es = sp·P^ep + sq·Q^eq` for integers,the residue is always in that table, for every `m`. So the filter never discards a solution.2. **Mordell reduction** (`mordell_i`, `mordell_ii`, `mordell_iii`). In each order of the signature `(3, 4, p)`,with `p = 6m + r`, a solution gives a rational point on the Mordell curve `Y² = X³ ± b^r`, where `b` is the baseof the `p`-th power: the three orders give `+C^r`, `+B^r` and `−B^r`.3. **Square conditions** (`sq_mod_four`, `sq_mod_nine`): the facts used in D8 to discard rows of the 2-adic and3-adic tables of Freitas–Naskręcki–Stoll when `x` in `x² + y³ = z¹³` is a perfect square.-/namespace BealSieve/-- The table built by the program is complete: `s^e mod m` is `t^e mod m` for some `t < m`. -/theorem pow_mod_mem_image (s e m : ℕ) (hm : 0 < m) :s ^ e % m ∈ (Finset.range m).image (fun t => t ^ e % m) :=Finset.mem_image.mpr ⟨s % m, Finset.mem_range.mpr (Nat.mod_lt _ hm), (Nat.pow_mod s e m).symm⟩/-- Soundness of the filter, for any signs `sp, sq` and any modulus `m`: if `S^es = sp·P^ep + sq·Q^eq` over `ℤ`,then `sp·P^ep + sq·Q^eq` is an `es`-th power in `ZMod m`. -/theorem filter_sound {S P Q sp sq : ℤ} {es ep eq : ℕ} (h : S ^ es = sp * P ^ ep + sq * Q ^ eq) (m : ℕ) :∃ t : ZMod m, t ^ es = (sp : ZMod m) * (P : ZMod m) ^ ep + (sq : ZMod m) * (Q : ZMod m) ^ eq :=⟨(S : ZMod m), byhave := congrArg (fun z : ℤ => (z : ZMod m)) hpush_cast at thisexact this⟩/-- Order `A³ + B⁴ = C^p`, `p = 6m + r`: the point `(−A/C^{2m}, B²/C^{3m})` lies on `Y² = X³ + C^r`. -/theorem mordell_i {A B C : ℤ} {m r : ℕ} (hC : C ≠ 0) (h : A ^ 3 + B ^ 4 = C ^ (6 * m + r)) :((B : ℚ) ^ 2 / (C : ℚ) ^ (3 * m)) ^ 2 = (-(A : ℚ) / (C : ℚ) ^ (2 * m)) ^ 3 + (C : ℚ) ^ r := byhave hC' : (C : ℚ) ≠ 0 := by exact_mod_cast hChave h' : (A : ℚ) ^ 3 + (B : ℚ) ^ 4 = (C : ℚ) ^ (6 * m + r) := by exact_mod_cast hhave e : (C : ℚ) ^ (6 * m + r) = ((C : ℚ) ^ m) ^ 6 * (C : ℚ) ^ r := by ringrw [e] at h'have hm : ((C : ℚ) ^ m) ≠ 0 := pow_ne_zero _ hC'rw [show (C : ℚ) ^ (3 * m) = ((C : ℚ) ^ m) ^ 3 by ring, show (C : ℚ) ^ (2 * m) = ((C : ℚ) ^ m) ^ 2 by ring]field_simplinear_combination h'/-- Order `A³ + B^p = C⁴`, `p = 6m + r`: the point `(A/B^{2m}, C²/B^{3m})` lies on `Y² = X³ + B^r`. -/theorem mordell_ii {A B C : ℤ} {m r : ℕ} (hB : B ≠ 0) (h : A ^ 3 + B ^ (6 * m + r) = C ^ 4) :((C : ℚ) ^ 2 / (B : ℚ) ^ (3 * m)) ^ 2 = ((A : ℚ) / (B : ℚ) ^ (2 * m)) ^ 3 + (B : ℚ) ^ r := byhave hB' : (B : ℚ) ≠ 0 := by exact_mod_cast hBhave h' : (A : ℚ) ^ 3 + (B : ℚ) ^ (6 * m + r) = (C : ℚ) ^ 4 := by exact_mod_cast hhave e : (B : ℚ) ^ (6 * m + r) = ((B : ℚ) ^ m) ^ 6 * (B : ℚ) ^ r := by ringrw [e] at h'have hm : ((B : ℚ) ^ m) ≠ 0 := pow_ne_zero _ hB'rw [show (B : ℚ) ^ (3 * m) = ((B : ℚ) ^ m) ^ 3 by ring, show (B : ℚ) ^ (2 * m) = ((B : ℚ) ^ m) ^ 2 by ring]field_simplinear_combination -h'/-- Order `A⁴ + B^p = C³`, `p = 6m + r`: the point `(C/B^{2m}, A²/B^{3m})` lies on `Y² = X³ − B^r`. -/theorem mordell_iii {A B C : ℤ} {m r : ℕ} (hB : B ≠ 0) (h : A ^ 4 + B ^ (6 * m + r) = C ^ 3) :((A : ℚ) ^ 2 / (B : ℚ) ^ (3 * m)) ^ 2 = ((C : ℚ) / (B : ℚ) ^ (2 * m)) ^ 3 - (B : ℚ) ^ r := byhave hB' : (B : ℚ) ≠ 0 := by exact_mod_cast hBhave h' : (A : ℚ) ^ 4 + (B : ℚ) ^ (6 * m + r) = (C : ℚ) ^ 3 := by exact_mod_cast hhave e : (B : ℚ) ^ (6 * m + r) = ((B : ℚ) ^ m) ^ 6 * (B : ℚ) ^ r := by ringrw [e] at h'have hm : ((B : ℚ) ^ m) ≠ 0 := pow_ne_zero _ hB'rw [show (B : ℚ) ^ (3 * m) = ((B : ℚ) ^ m) ^ 3 by ring, show (B : ℚ) ^ (2 * m) = ((B : ℚ) ^ m) ^ 2 by ring]field_simplinear_combination h'/-- Squares are `0` or `1` mod 4: a perfect square `x` is never `≡ 2` or `≡ 3 (mod 4)`. -/theorem sq_mod_four (u : ℤ) : u ^ 2 % 4 = 0 ∨ u ^ 2 % 4 = 1 := byhave h := Int.emod_emod_of_dvd u (by norm_num : (4 : ℤ) ∣ 4)rcases (by omega : u % 4 = 0 ∨ u % 4 = 1 ∨ u % 4 = 2 ∨ u % 4 = 3) with h0 | h0 | h0 | h0 <;>simp [pow_two, Int.mul_emod, h0]/-- Squares mod 9 are `0, 1, 4, 7`: a perfect square is never `≡ ±2, ±3 (mod 9)`; in particular `v₃(x) = 1`is impossible. -/theorem sq_mod_nine (u : ℤ) : u ^ 2 % 9 = 0 ∨ u ^ 2 % 9 = 1 ∨ u ^ 2 % 9 = 4 ∨ u ^ 2 % 9 = 7 := byrcases (by omega : u % 9 = 0 ∨ u % 9 = 1 ∨ u % 9 = 2 ∨ u % 9 = 3 ∨ u % 9 = 4 ∨ u % 9 = 5 ∨ u % 9 = 6 ∨u % 9 = 7 ∨ u % 9 = 8) with h0 | h0 | h0 | h0 | h0 | h0 | h0 | h0 | h0 <;>simp [pow_two, Int.mul_emod, h0]/-- D8 observation: if `a = u²`, the quadratic twist by `−2` of the Frey curve `y² = x³ + 3bx − 2a`, written as`Y² = X³ + 12bX + 16a`, has the rational point `(0, 4u)`. -/theorem twist_point (u b : ℤ) : (4 * u) ^ 2 = (0 : ℤ) ^ 3 + 12 * b * 0 + 16 * u ^ 2 := by ringend BealSieve#print axioms BealSieve.filter_sound#print axioms BealSieve.pow_mod_mem_image#print axioms BealSieve.mordell_i#print axioms BealSieve.mordell_ii#print axioms BealSieve.mordell_iii#print axioms BealSieve.sq_mod_four#print axioms BealSieve.sq_mod_nine