import Mathlib/-!# Beal's conjecture: cases that follow from Mathlib, reductions, and two lemmasBeal's conjecture: if `A^x + B^y = C^z` with positive integers and `x, y, z > 2`, then `A, B, C` have a commonprime factor. `bealConjecture` below is copied word for word from Formal Conjectures(`FormalConjectures/Wikipedia/BealConjecture.lean`), which also proves `bealConjecture → FermatLastTheorem`.Nothing here proves the conjecture. What is proved:* `not_sum_of_three_dvd`: no solutions at all when `3` divides all three exponents (Mathlib's FLT for `n = 3`).* `not_sum_of_four_four_two`: no solutions when `4 ∣ x`, `4 ∣ y`, `2 ∣ z` (Fermat: `a⁴ + b⁴ = c²` has none).* `not_sum_of_common_dvd`: assuming `FermatLastTheorem` (stated in Mathlib, not proved there), no solutions when thethree exponents have a common divisor `g ≥ 3`.* `coprime_of_gcd_eq_one`: in a counterexample `A, B, C` are pairwise coprime.* `bealConjecture_of_reduced`: it suffices to treat exponents in `{4} ∪ {odd primes}`.* `abc_quality_bound`: if `m ≤ x, y, z` then `(A·B·C)^m < (C^z)^3`, so a counterexample is an abc triple of qualitygreater than `m/3` (for `m = 5`, above the largest quality known).* `gcd_sum_cubes_factors`: the first step of the classical attack on `A³ + B³ = Cⁿ`.-/namespace BealPartial/-- Beal's conjecture, copied from Formal Conjectures (`BealConjecture.bealConjecture`). -/def bealConjecture : Prop := ∀ {A B C x y z : ℕ},A ≠ 0 → B ≠ 0 → C ≠ 0 → 2 < x → 2 < y → 2 < z →A^x + B^y = C^z → 1 < Finset.gcd {A, B, C} id/-- Beal's conclusion for one exponent triple `(x, y, z)`. -/def BealFor (x y z : ℕ) : Prop := ∀ {A B C : ℕ},A ≠ 0 → B ≠ 0 → C ≠ 0 → A^x + B^y = C^z → 1 < Finset.gcd {A, B, C} idtheorem bealConjecture_iff :bealConjecture ↔ ∀ x y z : ℕ, 2 < x → 2 < y → 2 < z → BealFor x y z := byconstructor· intro h x y z hx hy hz A B C hA hB hC hsexact h hA hB hC hx hy hz hs· intro h A B C x y z hA hB hC hx hy hz hsexact h x y z hx hy hz hA hB hC hstheorem gcd_triple (A B C : ℕ) : Finset.gcd {A, B, C} id = Nat.gcd A (Nat.gcd B C) := bysimp [Finset.gcd_insert, Finset.gcd_singleton]rfl/-! ## Cases that follow from Fermat's Last Theorem for exponents 3 and 4 (both in Mathlib) -//-- If `3` divides all three exponents there is no solution at all: `fermatLastTheoremThree`. -/theorem not_sum_of_three_dvd {A B C x y z : ℕ} (hA : A ≠ 0) (hB : B ≠ 0) (hC : C ≠ 0)(hx : 3 ∣ x) (hy : 3 ∣ y) (hz : 3 ∣ z) : A ^ x + B ^ y ≠ C ^ z := byobtain ⟨a, rfl⟩ := hxobtain ⟨b, rfl⟩ := hyobtain ⟨c, rfl⟩ := hzrw [pow_mul', pow_mul', pow_mul']exact fermatLastTheoremThree (A ^ a) (B ^ b) (C ^ c)(pow_ne_zero _ hA) (pow_ne_zero _ hB) (pow_ne_zero _ hC)/-- If `4 ∣ x`, `4 ∣ y` and `2 ∣ z` there is no solution: `a⁴ + b⁴ = c²` has none (`not_fermat_42`). -/theorem not_sum_of_four_four_two {A B C x y z : ℕ} (hA : A ≠ 0) (hB : B ≠ 0)(hx : 4 ∣ x) (hy : 4 ∣ y) (hz : 2 ∣ z) : A ^ x + B ^ y ≠ C ^ z := byobtain ⟨a, rfl⟩ := hxobtain ⟨b, rfl⟩ := hyobtain ⟨c, rfl⟩ := hzintro hrw [pow_mul', pow_mul', pow_mul'] at hhave h' : ((A ^ a : ℕ) : ℤ) ^ 4 + ((B ^ b : ℕ) : ℤ) ^ 4 = ((C ^ c : ℕ) : ℤ) ^ 2 := byexact_mod_cast hexact not_fermat_42 (by exact_mod_cast pow_ne_zero a hA) (by exact_mod_cast pow_ne_zero b hB) h'/-- Assuming Fermat's Last Theorem (stated in Mathlib, not proved there): a common divisor `g ≥ 3` of the threeexponents rules out solutions. -/theorem not_sum_of_common_dvd (hFLT : FermatLastTheorem) {A B C x y z g : ℕ}(hA : A ≠ 0) (hB : B ≠ 0) (hC : C ≠ 0)(hg : 3 ≤ g) (hx : g ∣ x) (hy : g ∣ y) (hz : g ∣ z) : A ^ x + B ^ y ≠ C ^ z := byobtain ⟨a, rfl⟩ := hxobtain ⟨b, rfl⟩ := hyobtain ⟨c, rfl⟩ := hzrw [pow_mul', pow_mul', pow_mul']exact hFLT g hg (A ^ a) (B ^ b) (C ^ c) (pow_ne_zero _ hA) (pow_ne_zero _ hB) (pow_ne_zero _ hC)theorem bealFor_of_three_dvd {x y z : ℕ} (hx : 3 ∣ x) (hy : 3 ∣ y) (hz : 3 ∣ z) : BealFor x y z := byintro A B C hA hB hC hsexact absurd hs (not_sum_of_three_dvd hA hB hC hx hy hz)theorem bealFor_of_four_four_two {x y z : ℕ} (hx : 4 ∣ x) (hy : 4 ∣ y) (hz : 2 ∣ z) : BealFor x y z := byintro A B C hA hB _ hsexact absurd hs (not_sum_of_four_four_two hA hB hx hy hz)theorem bealFor_of_flt (hFLT : FermatLastTheorem) {x y z g : ℕ} (hg : 3 ≤ g)(hx : g ∣ x) (hy : g ∣ y) (hz : g ∣ z) : BealFor x y z := byintro A B C hA hB hC hsexact absurd hs (not_sum_of_common_dvd hFLT hA hB hC hg hx hy hz)/-! ## Structure of a counterexample -//-- If no prime divides all of `A, B, C` and `A^x + B^y = C^z` (positive exponents), they are pairwise coprime. -/theorem coprime_of_gcd_eq_one {A B C x y z : ℕ} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0)(h : A ^ x + B ^ y = C ^ z) (hg : Nat.gcd A (Nat.gcd B C) = 1) :Nat.Coprime A B ∧ Nat.Coprime A C ∧ Nat.Coprime B C := byrefine ⟨Nat.coprime_of_dvd fun p hp hpA hpB => ?_, Nat.coprime_of_dvd fun p hp hpA hpC => ?_,Nat.coprime_of_dvd fun p hp hpB hpC => ?_⟩· have hpC : p ∣ C := hp.dvd_of_dvd_pow (h ▸ dvd_add (dvd_pow hpA hx) (dvd_pow hpB hy))have h1 : p ∣ Nat.gcd A (Nat.gcd B C) := Nat.dvd_gcd hpA (Nat.dvd_gcd hpB hpC)rw [hg] at h1exact hp.one_lt.ne' (Nat.dvd_one.mp h1)· have hpB : p ∣ B := byhave hs : p ∣ A ^ x + B ^ y := h ▸ dvd_pow hpC hzexact hp.dvd_of_dvd_pow ((Nat.dvd_add_right (dvd_pow hpA hx)).mp hs)have h1 : p ∣ Nat.gcd A (Nat.gcd B C) := Nat.dvd_gcd hpA (Nat.dvd_gcd hpB hpC)rw [hg] at h1exact hp.one_lt.ne' (Nat.dvd_one.mp h1)· have hpA : p ∣ A := byhave hs : p ∣ A ^ x + B ^ y := h ▸ dvd_pow hpC hzexact hp.dvd_of_dvd_pow ((Nat.dvd_add_left (dvd_pow hpB hy)).mp hs)have h1 : p ∣ Nat.gcd A (Nat.gcd B C) := Nat.dvd_gcd hpA (Nat.dvd_gcd hpB hpC)rw [hg] at h1exact hp.one_lt.ne' (Nat.dvd_one.mp h1)/-! ## Reduction of the exponents -//-- Every `n ≥ 3` has a divisor that is `4` or an odd prime. -/theorem exists_dvd_four_or_odd_prime {n : ℕ} (hn : 3 ≤ n) :∃ d, d ∣ n ∧ (d = 4 ∨ (d.Prime ∧ Odd d)) := byrcases Nat.eq_two_pow_or_exists_odd_prime_and_dvd n with ⟨k, rfl⟩ | ⟨p, hp, hpn, hodd⟩· refine ⟨4, ?_, Or.inl rfl⟩have hk : 2 ≤ k := byrcases Nat.lt_or_ge k 2 with hk | hk· interval_cases k <;> simp at hn· exact hkrefine ⟨2 ^ (k - 2), ?_⟩rw [show (4 : ℕ) = 2 ^ 2 by norm_num, ← pow_add]congr 1omega· exact ⟨p, hpn, Or.inr ⟨hp, hodd⟩⟩/-- A counterexample for exponents `(x, y, z)` gives one for any divisors `(d, e, f)`. -/theorem bealFor_of_dvd {x y z d e f : ℕ} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0)(hd : d ∣ x) (he : e ∣ y) (hf : f ∣ z) (H : BealFor d e f) : BealFor x y z := byintro A B C hA hB hC hsobtain ⟨a, rfl⟩ := hdobtain ⟨b, rfl⟩ := heobtain ⟨c, rfl⟩ := hfrw [pow_mul', pow_mul', pow_mul'] at hshave h1 := H (pow_ne_zero a hA) (pow_ne_zero b hB) (pow_ne_zero c hC) hsrw [gcd_triple] at h1 ⊢obtain ⟨p, hp, hpg⟩ := Nat.exists_prime_and_dvd (ne_of_gt h1)have hpA : p ∣ A := hp.dvd_of_dvd_pow (hpg.trans (Nat.gcd_dvd_left _ _))have hpB : p ∣ B :=hp.dvd_of_dvd_pow ((hpg.trans (Nat.gcd_dvd_right _ _)).trans (Nat.gcd_dvd_left _ _))have hpC : p ∣ C :=hp.dvd_of_dvd_pow ((hpg.trans (Nat.gcd_dvd_right _ _)).trans (Nat.gcd_dvd_right _ _))have hpos : 0 < Nat.gcd A (Nat.gcd B C) := Nat.gcd_pos_of_pos_left _ (Nat.pos_of_ne_zero hA)exact lt_of_lt_of_le hp.one_lt (Nat.le_of_dvd hpos (Nat.dvd_gcd hpA (Nat.dvd_gcd hpB hpC)))/-- Beal's conjecture follows from its cases with exponents in `{4} ∪ {odd primes}`. -/theorem bealConjecture_of_reduced(H : ∀ p q r : ℕ, (p = 4 ∨ (p.Prime ∧ Odd p)) → (q = 4 ∨ (q.Prime ∧ Odd q)) →(r = 4 ∨ (r.Prime ∧ Odd r)) → BealFor p q r) : bealConjecture := byrw [bealConjecture_iff]intro x y z hx hy hzobtain ⟨d, hd, hd'⟩ := exists_dvd_four_or_odd_prime hxobtain ⟨e, he, he'⟩ := exists_dvd_four_or_odd_prime hyobtain ⟨f, hf, hf'⟩ := exists_dvd_four_or_odd_prime hzexact bealFor_of_dvd (by omega) (by omega) (by omega) hd he hf (H d e f hd' he' hf')/-! ## A counterexample is an abc triple of high quality -//-- If `m ≤ x, y, z` and `A^x + B^y = C^z` with `A, B ≥ 1`, then `(A·B·C)^m < (C^z)^3`. Since`rad(A^x B^y C^z) = rad(ABC) ≤ ABC`, the abc triple `(A^x, B^y, C^z)` then has quality`log C^z / log rad > m/3`. -/theorem abc_quality_bound {A B C x y z m : ℕ} (hA : A ≠ 0) (hB : B ≠ 0)(hmx : m ≤ x) (hmy : m ≤ y) (hmz : m ≤ z) (h : A ^ x + B ^ y = C ^ z) :(A * B * C) ^ m < (C ^ z) ^ 3 := byhave hAx0 : 0 < A ^ x := Nat.pos_of_ne_zero (pow_ne_zero x hA)have hBy0 : 0 < B ^ y := Nat.pos_of_ne_zero (pow_ne_zero y hB)have hAx : A ^ x < C ^ z := by omegahave hBy : B ^ y < C ^ z := by omegahave hC : C ≠ 0 := byrintro rflrcases Nat.eq_zero_or_pos z with hz | hz· subst hzrw [pow_zero] at hAxomega· rw [zero_pow hz.ne'] at hAxomegahave hAm : A ^ m < C ^ z := lt_of_le_of_lt (Nat.pow_le_pow_right (Nat.pos_of_ne_zero hA) hmx) hAxhave hBm : B ^ m < C ^ z := lt_of_le_of_lt (Nat.pow_le_pow_right (Nat.pos_of_ne_zero hB) hmy) hByhave hCm : C ^ m ≤ C ^ z := Nat.pow_le_pow_right (Nat.pos_of_ne_zero hC) hmzhave hCz : 0 < C ^ z := lt_of_le_of_lt (Nat.zero_le _) hAxcalc (A * B * C) ^ m = A ^ m * B ^ m * C ^ m := by rw [mul_pow, mul_pow]_ < C ^ z * C ^ z * C ^ z :=Nat.mul_lt_mul_of_lt_of_le (Nat.mul_lt_mul_of_lt_of_le hAm hBm.le hCz) hCm hCz_ = (C ^ z) ^ 3 := by ring/-! ## The first step of the classical attack on `A³ + B³ = Cⁿ` -//-- For coprime integers `A, B`, the factors `A + B` and `A² − AB + B²` of `A³ + B³` have gcd dividing `3`.So a primitive solution of `A³ + B³ = Cⁿ` makes each factor an `n`-th power up to a power of `3`. -/theorem gcd_sum_cubes_factors {A B : ℤ} (h : IsCoprime A B) :((Int.gcd (A + B) (A ^ 2 - A * B + B ^ 2) : ℕ) : ℤ) ∣ 3 := byhave h1 : ((Int.gcd (A + B) (A ^ 2 - A * B + B ^ 2) : ℕ) : ℤ) ∣ A + B := Int.gcd_dvd_left ..have h2 : ((Int.gcd (A + B) (A ^ 2 - A * B + B ^ 2) : ℕ) : ℤ) ∣ A ^ 2 - A * B + B ^ 2 :=Int.gcd_dvd_right ..have hB3 : ((Int.gcd (A + B) (A ^ 2 - A * B + B ^ 2) : ℕ) : ℤ) ∣ 3 * B ^ 2 := byhave e : 3 * B ^ 2 = (A ^ 2 - A * B + B ^ 2) - (A + B) * (A - 2 * B) := by ringrw [e]exact dvd_sub h2 (dvd_mul_of_dvd_left h1 _)have hA3 : ((Int.gcd (A + B) (A ^ 2 - A * B + B ^ 2) : ℕ) : ℤ) ∣ 3 * A ^ 2 := byhave e : 3 * A ^ 2 = (A ^ 2 - A * B + B ^ 2) - (A + B) * (B - 2 * A) := by ringrw [e]exact dvd_sub h2 (dvd_mul_of_dvd_left h1 _)obtain ⟨u, v, huv⟩ := h.pow (m := 2) (n := 2)have e3 : (3 : ℤ) = u * (3 * A ^ 2) + v * (3 * B ^ 2) := by linear_combination (-3 : ℤ) * huvrw [e3]exact dvd_add (dvd_mul_of_dvd_right hA3 _) (dvd_mul_of_dvd_right hB3 _)end BealPartial/-! ## Axiom check -/#print axioms BealPartial.not_sum_of_three_dvd#print axioms BealPartial.not_sum_of_four_four_two#print axioms BealPartial.not_sum_of_common_dvd#print axioms BealPartial.coprime_of_gcd_eq_one#print axioms BealPartial.bealConjecture_of_reduced#print axioms BealPartial.abc_quality_bound#print axioms BealPartial.gcd_sum_cubes_factors