import Mathlib/-!# Beal's conjecture: local constraints learned by a logic network and proved in Lean`TableD p ex ey ez`: no nonzero `a, b, c` in `ZMod p` with `a^ex + b^ey = c^ez`. If it holds and`x ≡ ex`, `y ≡ ey`, `z ≡ ez (mod p - 1)`, then every solution of `A^x + B^y = C^z` has `p ∣ A·B·C`(`dvd_of_tableD`, by Fermat's little theorem). A logic network (gates as in Petersen et al. 2022, the E35/E36code of this project) learned the rule "TableD holds" from the divisibility bits `[d ∣ e]`, `d ∣ p - 1`. For`p = 5` and `p = 7` all 10 trained circuits compute the same Boolean function (checked in Python on every input);`rule5` and `rule7` below are that function, and `decide` proves it equal to `TableD` on every residue triple.GENERATED by b1_lean.py from b1_circuits.json; do not edit by hand.-/namespace BealLocal/-- No nonzero `a, b, c` mod `p` with `a^ex + b^ey = c^ez`. -/def TableD (p ex ey ez : ℕ) : Prop :=∀ a b c : ZMod p, a ≠ 0 → b ≠ 0 → c ≠ 0 → a ^ ex + b ^ ey ≠ c ^ ez/-- The same check on natural numbers (fast in the kernel: `Nat` arithmetic on literals is native). -/def tableN (p ex ey ez : ℕ) : Bool :=(List.range p).all fun a => a == 0 || (List.range p).all fun b => b == 0 ||(List.range p).all fun c => c == 0 || (a ^ ex + b ^ ey) % p != c ^ ez % ptheorem tableD_of_tableN {p ex ey ez : ℕ} [NeZero p] (h : tableN p ex ey ez = true) :TableD p ex ey ez := byintro a b c ha hb hc habcsimp only [tableN, List.all_eq_true, List.mem_range, Bool.or_eq_true, beq_iff_eq, bne_iff_ne,ne_eq] at hhave key' : ((a.val ^ ex + b.val ^ ey : ℕ) : ZMod p) = ((c.val ^ ez : ℕ) : ZMod p) := bypush_castrw [ZMod.natCast_zmod_val, ZMod.natCast_zmod_val, ZMod.natCast_zmod_val]exact habchave key := (ZMod.natCast_eq_natCast_iff' _ _ _).mp key'rcases h a.val (ZMod.val_lt a) with h0 | h1· exact ha ((ZMod.val_eq_zero a).mp h0)rcases h1 b.val (ZMod.val_lt b) with h0 | h2· exact hb ((ZMod.val_eq_zero b).mp h0)rcases h2 c.val (ZMod.val_lt c) with h0 | h3· exact hc ((ZMod.val_eq_zero c).mp h0)exact h3 key/-- Fermat's little theorem, in the form used here: for `a ≠ 0` in `ZMod p`, `a^n = a^(n % (p - 1))`. -/theorem pow_mod_card_sub_one {p : ℕ} [Fact p.Prime] {a : ZMod p} (ha : a ≠ 0) (n : ℕ) :a ^ n = a ^ (n % (p - 1)) := byconv_lhs => rw [← Nat.div_add_mod n (p - 1), pow_add, pow_mul, ZMod.pow_card_sub_one_eq_one ha, one_pow,one_mul]theorem natCast_ne_zero_of_not_dvd {p A : ℕ} (h : ¬ p ∣ A) : (A : ZMod p) ≠ 0 := byrwa [Ne, ZMod.natCast_eq_zero_iff]/-- The local rule gives a divisor: if `TableD p ex ey ez` and the exponents are `≡ ex, ey, ez (mod p - 1)`,every solution of `A^x + B^y = C^z` has `p ∣ A·B·C`. -/theorem dvd_of_tableD {p : ℕ} [Fact p.Prime] {ex ey ez A B C x y z : ℕ} (hT : TableD p ex ey ez)(hx : x % (p - 1) = ex) (hy : y % (p - 1) = ey) (hz : z % (p - 1) = ez)(h : A ^ x + B ^ y = C ^ z) : p ∣ A * B * C := byby_contra hdhave hA : (A : ZMod p) ≠ 0 :=natCast_ne_zero_of_not_dvd fun h' => hd (dvd_mul_of_dvd_left (dvd_mul_of_dvd_left h' _) _)have hB : (B : ZMod p) ≠ 0 :=natCast_ne_zero_of_not_dvd fun h' => hd (dvd_mul_of_dvd_left (dvd_mul_of_dvd_right h' _) _)have hC : (C : ZMod p) ≠ 0 := natCast_ne_zero_of_not_dvd fun h' => hd (dvd_mul_of_dvd_right h' _)have hc : (A : ZMod p) ^ x + (B : ZMod p) ^ y = (C : ZMod p) ^ z := by exact_mod_cast congrArg (Nat.cast : ℕ → ZMod p) hrw [pow_mod_card_sub_one hA, pow_mod_card_sub_one hB, pow_mod_card_sub_one hC, hx, hy, hz] at hcexact hT _ _ _ hA hB hC hc/-- If `p - 1` divides `y` and `z` (and `x ≠ 0`), every solution of `A^x + B^y = C^z` has `p ∣ A·B·C`:otherwise `B^y ≡ C^z ≡ 1`, so `A^x ≡ 0`. -/theorem dvd_of_card_sub_one_dvd_yz {p : ℕ} [Fact p.Prime] {A B C x y z : ℕ} (hx : x ≠ 0)(hy : (p - 1) ∣ y) (hz : (p - 1) ∣ z) (h : A ^ x + B ^ y = C ^ z) : p ∣ A * B * C := byby_contra hdhave hA : (A : ZMod p) ≠ 0 :=natCast_ne_zero_of_not_dvd fun h' => hd (dvd_mul_of_dvd_left (dvd_mul_of_dvd_left h' _) _)have hB : (B : ZMod p) ≠ 0 :=natCast_ne_zero_of_not_dvd fun h' => hd (dvd_mul_of_dvd_left (dvd_mul_of_dvd_right h' _) _)have hC : (C : ZMod p) ≠ 0 := natCast_ne_zero_of_not_dvd fun h' => hd (dvd_mul_of_dvd_right h' _)have hc : (A : ZMod p) ^ x + (B : ZMod p) ^ y = (C : ZMod p) ^ z := by exact_mod_cast congrArg (Nat.cast : ℕ → ZMod p) hrw [pow_mod_card_sub_one hB y, pow_mod_card_sub_one hC z, Nat.mod_eq_zero_of_dvd hy,Nat.mod_eq_zero_of_dvd hz, pow_zero, pow_zero] at hchave h0 : (A : ZMod p) ^ x = 0 := by linear_combination hcexact hA ((pow_eq_zero_iff hx).mp h0)/-! ## p = 5: the rule learned by the logic network -//-- The Boolean function of the learned circuits for p = 5 (`(x4 & z4) | (y4 & z4) | (x2 & y2 & z2)`,where `x4` means `4 ∣ x`, etc.). -/def rule5 (ex ey ez : ℕ) : Bool :=((ex % 4 == 0) && (ez % 4 == 0)) || ((ey % 4 == 0) && (ez % 4 == 0)) || ((ex % 2 == 0) && (ey % 2 == 0) && (ez % 2 == 0))/-- The learned rule says "TableD" exactly where it holds. -/theorem rule5_exact : ∀ ex < 4, ∀ ey < 4, ∀ ez < 4,rule5 ex ey ez = tableN 5 ex ey ez := by decide +kernel/-- Where the learned rule says yes, every solution of `A^x + B^y = C^z` has 5 ∣ A·B·C. -/theorem rule5_sound {A B C x y z : ℕ} (hr : rule5 (x % 4) (y % 4) (z % 4) = true)(h : A ^ x + B ^ y = C ^ z) : 5 ∣ A * B * C := byhave : Fact (Nat.Prime 5) := ⟨by norm_num⟩have hT : TableD 5 (x % 4) (y % 4) (z % 4) := byhave := rule5_exact (x % 4) (Nat.mod_lt _ (by norm_num)) (y % 4) (Nat.mod_lt _ (by norm_num))(z % 4) (Nat.mod_lt _ (by norm_num))rw [hr] at thisexact tableD_of_tableN this.symmexact dvd_of_tableD hT rfl rfl rfl h/-! ## p = 7: the rule learned by the logic network -//-- The Boolean function of the learned circuits for p = 7 (`(x6 & z6) | (y6 & z6) | (x2 & y6 & z3) | (x3 & y3 & z3) | (x6 & y2 & z3)`,where `x6` means `6 ∣ x`, etc.). -/def rule7 (ex ey ez : ℕ) : Bool :=((ex % 6 == 0) && (ez % 6 == 0)) || ((ey % 6 == 0) && (ez % 6 == 0)) || ((ex % 2 == 0) && (ey % 6 == 0) && (ez % 3 == 0)) || ((ex % 3 == 0) && (ey % 3 == 0) && (ez % 3 == 0)) || ((ex % 6 == 0) && (ey % 2 == 0) && (ez % 3 == 0))/-- The learned rule says "TableD" exactly where it holds. -/theorem rule7_exact : ∀ ex < 6, ∀ ey < 6, ∀ ez < 6,rule7 ex ey ez = tableN 7 ex ey ez := by decide +kernel/-- Where the learned rule says yes, every solution of `A^x + B^y = C^z` has 7 ∣ A·B·C. -/theorem rule7_sound {A B C x y z : ℕ} (hr : rule7 (x % 6) (y % 6) (z % 6) = true)(h : A ^ x + B ^ y = C ^ z) : 7 ∣ A * B * C := byhave : Fact (Nat.Prime 7) := ⟨by norm_num⟩have hT : TableD 7 (x % 6) (y % 6) (z % 6) := byhave := rule7_exact (x % 6) (Nat.mod_lt _ (by norm_num)) (y % 6) (Nat.mod_lt _ (by norm_num))(z % 6) (Nat.mod_lt _ (by norm_num))rw [hr] at thisexact tableD_of_tableN this.symmexact dvd_of_tableD hT rfl rfl rfl h/-! ## Consequences stated with divisibility -//-- All three exponents even: 5 divides `A·B·C` (squares of units mod 5 are ±1). -/theorem five_dvd_of_even {A B C x y z : ℕ} (hx : 2 ∣ x) (hy : 2 ∣ y) (hz : 2 ∣ z)(h : A ^ x + B ^ y = C ^ z) : 5 ∣ A * B * C := byapply rule5_sound _ hhave h1 : x % 4 % 2 = 0 := by omegahave h2 : y % 4 % 2 = 0 := by omegahave h3 : z % 4 % 2 = 0 := by omegasimp [rule5, h1, h2, h3]/-- All three exponents divisible by 3: 7 divides `A·B·C` (cubes of units mod 7 are ±1). -/theorem seven_dvd_of_three_dvd {A B C x y z : ℕ} (hx : 3 ∣ x) (hy : 3 ∣ y) (hz : 3 ∣ z)(h : A ^ x + B ^ y = C ^ z) : 7 ∣ A * B * C := byapply rule7_sound _ hhave h1 : x % 6 % 3 = 0 := by omegahave h2 : y % 6 % 3 = 0 := by omegahave h3 : z % 6 % 3 = 0 := by omegasimp [rule7, h1, h2, h3]/-- `2 ∣ x`, `6 ∣ y`, `3 ∣ z`: 7 divides `A·B·C` (squares + 1 never hit ±1 mod 7). -/theorem seven_dvd_of_two_six_three {A B C x y z : ℕ} (hx : 2 ∣ x) (hy : 6 ∣ y) (hz : 3 ∣ z)(h : A ^ x + B ^ y = C ^ z) : 7 ∣ A * B * C := byapply rule7_sound _ hhave h1 : x % 6 % 2 = 0 := by omegahave h2 : y % 6 % 6 = 0 := by omegahave h3 : z % 6 % 3 = 0 := by omegasimp [rule7, h1, h2, h3]/-! ## Instances: Sophie Germain type divisors found by the scan -/theorem tableD_7_333 : TableD 7 3 3 3 := tableD_of_tableN (by decide +kernel)theorem tableD_13_333 : TableD 13 3 3 3 := tableD_of_tableN (by decide +kernel)theorem tableD_11_555 : TableD 11 5 5 5 := tableD_of_tableN (by decide +kernel)/-- In any solution of `A^4 + B^y = C^4` with `y ≥ 1`, both 3 and 5 divide `A·B·C` (`p - 1` divides 4). -/theorem fifteen_dvd_of_four_y_four {A B C y : ℕ} (hy : y ≠ 0) (h : A ^ 4 + B ^ y = C ^ 4) :3 ∣ A * B * C ∧ 5 ∣ A * B * C := byhave h' : B ^ y + A ^ 4 = C ^ 4 := by rw [add_comm]; exact hhave : Fact (Nat.Prime 3) := ⟨by norm_num⟩have h3 := dvd_of_card_sub_one_dvd_yz (p := 3) hy (by norm_num) (by norm_num) h'have : Fact (Nat.Prime 5) := ⟨by norm_num⟩have h5 := dvd_of_card_sub_one_dvd_yz (p := 5) hy (by norm_num) (by norm_num) h'have e : B * A * C = A * B * C := by ringexact ⟨e ▸ h3, e ▸ h5⟩end BealLocal#print axioms BealLocal.dvd_of_tableD#print axioms BealLocal.dvd_of_card_sub_one_dvd_yz#print axioms BealLocal.tableD_of_tableN#print axioms BealLocal.rule5_exact#print axioms BealLocal.rule7_exact#print axioms BealLocal.rule5_sound#print axioms BealLocal.rule7_sound#print axioms BealLocal.five_dvd_of_even#print axioms BealLocal.seven_dvd_of_three_dvd#print axioms BealLocal.seven_dvd_of_two_six_three#print axioms BealLocal.tableD_11_555#print axioms BealLocal.fifteen_dvd_of_four_y_four