arrangement3_e4_zero_iff_is_magic
An arrangement of order 3 satisfies the four-term line-sum energy zero condition $E_4(S) = 0$ if and only if it is a magic square.
Statement
theorem arrangement3_e4_zero_iff_is_magic (S : Fin 3 × Fin 3 ≃ Fin 9) :
arrangement3_e4_zero S ↔ is_magic_arrangement3 S- Formalizes
- arXiv:2610.38386, Proposition 5.1
- Verified
- 02 Nov 2026
- Axioms
Classical.choiceQuot.soundpropext- Built on 7
- is_magic_arrangement3An arrangement $S : \mathrm{Fin}\,3 \times \mathrm{Fin}\,3 \simeq \mathrm{Fin}\,9$ is a magic square if every row sum…
- arrangement3_e4_zeroThe four-term line-sum energy zero condition $E_4(S) = 0$ for an arrangement…
- arrangement3_e4_zero_of_is_magicEvery magic square of order 3 satisfies the four-term line-sum energy zero condition $E_4(S) = 0$.
- arrangement3_diophantine_systemFor integers $\kappa, a', c', \xi', \eta'$ satisfying $\xi' + \eta' = \kappa$, $4\kappa^2 + 3(a'^2 + c'^2) + \xi'^2 + \eta'^2 = 30$, and…
- arrangement3_sum_entriesThe sum of all entries in any 3 × 3 arrangement of the integers 1 through 9 (without repetition) is 45.
- TotalPreorderA total preorder: a preorder where
≤is total (complete). This is weaker thanLinearOrder— it does NOT require antisymmetry or… - LinearOrder.toTotalPreorderEvery
LinearOrderis aTotalPreorder.
Read back from the Lean
For every bijection $S$ from $\text{Fin } 3 \times \text{Fin } 3$ to $\text{Fin } 9$, the arrangement condition arrangement3_e4_zero S holds if and only if is_magic_arrangement3 S holds.
Written by a model that saw only the Lean, never the English above. If the two disagree, that is worth a challenge.
The proof, explained
The theorem is proved by centering the entries of the order 3 arrangement around their mean 5, which transforms the four energy-zero conditions (row/column first moments and diagonal sums) and the sum-of-squares constraint into a centered Diophantine system. In the helper lemma arrangement3_e4_zero_iff_is_magic_core, the linear relations force the center entry to be an even integer -2κ and express the sum of squares as a positive-definite quadratic form equal to 30. Injectivity of the arrangement ensures the corner entries satisfy non-degeneracy conditions, allowing arrangement3_diophantine_system to deduce that κ = 0 and that all row and column sums vanish. Conversely, every magic square satisfies the energy-zero conditions by arrangement3_e4_zero_of_is_magic.
The center entry p₁₁ is an even integer -2κ, and the sum of the first row η and first column ξ satisfies ξ + η = 3κ.
Let ξ = p₀₀ + p₁₀ + p₂₀ and η = p₀₀ + p₀₁ + p₀₂ be the first column and row sums. Combining the linear constraints on total sum, moments, and diagonals via linear arithmetic yields 3p₁₁ = -2(ξ + η). Divisibility arithmetic implies 2 ∣ p₁₁, so writing p₁₁ = -2κ for an integer κ = -μ gives ξ + η = 3κ.
set ξ := p00+p10+p20 with hξ set η := p00+p01+p02 with hη have hRel : 3*p11 = -2*(ξ+η) := by linarith have hdvd : 2 ∣ p11 := by omega obtain ⟨μ, hμ⟩ := hdvd have hκ : p11 = -2*(-μ) := by linarith set κ := -μ with hκdef have hξη : ξ + η = 3*κ := by linarith
The opposite corners along each diagonal sum to 2κ, and every entry is determined linearly by p₀₀, p₀₂, κ, ξ, and η.
From the main diagonal sum p₀₀ + p₁₁ + p₂₂ = 0 and anti-diagonal sum p₀₂ + p₁₁ + p₂₀ = 0 with p₁₁ = -2κ, linear arithmetic gives p₀₀ + p₂₂ = 2κ and p₀₂ + p₂₀ = 2κ. The remaining entries p₂₂, p₂₀, p₀₁, p₁₀, p₁₂, p₂₁ and the variable η are then solved in terms of p₀₀, p₀₂, κ, and ξ.
have hκA : 2*κ = p00 + p22 := by linarith have hκB : 2*κ = p02 + p20 := by linarith have h22 : p22 = -p00 - p11 := by linarith have h20 : p20 = -p02 - p11 := by linarith have h01 : p01 = η - p00 - p02 := by linarith have h10 : p10 = ξ - p00 - p20 := by linarith have h12 : p12 = ξ - p02 - p22 := by linarith have h21 : p21 = -2*ξ - p11 - p01 := by linarith have hη' : η = 3*κ - ξ := by linarith
The sum-of-squares condition is equivalent to 4κ² + 3((p₀₀ - κ)² + (p₀₂ - κ)²) + (ξ - κ)² + (η - κ)² = 30.
Substituting the linear expressions for all nine entries into the sum of squares ∑ pᵢⱼ² reveals, by polynomial ring expansion, that the sum is exactly twice 4κ² + 3((p₀₀ - κ)² + (p₀₂ - κ)²) + (ξ - κ)² + (η - κ)². Since the total sum of squares is 60, dividing by 2 yields 30.
have heq : 4*κ^2+3*((p00-κ)^2+(p02-κ)^2)+(ξ-κ)^2+(η-κ)^2 = 30 := by have hident : p00^2+p01^2+p02^2+p10^2+p11^2+p12^2+p20^2+p21^2+p22^2 = 2*(4*κ^2+3*((p00-κ)^2+(p02-κ)^2)+(ξ-κ)^2+(η-κ)^2) := by simp only [h22, h20, h01, h12, h21, h10, hκ, hη']; ring linarith [hsq, hident]
The shifted corner variables satisfy p₀₀ - κ ≠ 0, p₀₂ - κ ≠ 0, and (p₀₀ - κ)² ≠ (p₀₂ - κ)².
If p₀₀ - κ = 0, then p₀₀ + p₂₂ = 2κ = 2p₀₀ implies p₀₀ = p₂₂, contradicting the hypothesis p₀₀ ≠ p₂₂. Similarly, p₀₂ - κ = 0 would imply p₀₂ = p₂₀, contradicting p₀₂ ≠ p₂₀. If their squares were equal, the difference of squares ((p₀₀ - κ) - (p₀₂ - κ))((p₀₀ - κ) + (p₀₂ - κ)) would vanish. By mul_eq_zero.mp, one factor must be zero, but the difference is p₀₀ - p₀₂ ≠ 0 and the sum is p₀₂ - p₂₂ ≠ 0 by the distinct corner hypotheses.
uses
mul_eq_zero.mphave hc1 : p00 - κ ≠ 0 := by intro h; apply hne1; linarith have hc2 : p02 - κ ≠ 0 := by intro h; apply hne2; linarith have hc3 : (p00-κ)^2 ≠ (p02-κ)^2 := by intro hh have hprod : ((p00-κ)-(p02-κ))*((p00-κ)+(p02-κ)) = 0 := by nlinarith [hh] have hsub : (p00-κ)-(p02-κ) ≠ 0 := by intro h; apply hne3; linarith have hadd : (p00-κ)+(p02-κ) ≠ 0 := by intro h; apply hne4; linarith rcases mul_eq_zero.mp hprod with h | h · exact hsub h · exact hadd h
Every row sum and column sum of the matrix is zero.
Applying arrangement3_diophantine_system to κ, the shifted corners, and the shifted sums (which sum to κ and satisfy the quadratic equation 30 with non-degenerate corners) yields κ = 0, ξ - κ = 0, and η - κ = 0. Hence κ = 0, ξ = 0, and η = 0, from which linear arithmetic concludes that all three row sums and all three column sums vanish.
uses
arrangement3_diophantine_systemobtain ⟨hκ0, hξ', hη'0⟩ := arrangement3_diophantine_system κ (p00-κ) (p02-κ) (ξ-κ) (η-κ) (by linarith) heq ⟨hc1, hc2, hc3⟩ have hξ0 : ξ = 0 := by linarith have hη0 : η = 0 := by linarith refine ⟨by linarith, by linarith, by linarith, by linarith, by linarith, by linarith⟩
In the forward direction, define the centered entries Z(p) = (S(p) + 1) - 5.
We split the equivalence into two implications. Assuming the four energy-zero conditions of E₄(S) = 0 (column and row moments equal 45, diagonal sums equal 15), we center the 1-based entries S(p) + 1 around their mean value 5 by defining Z(p) = (S(p) + 1) - 5.
constructor · intro h obtain ⟨hcol, hrow, hdiag, hanti⟩ := h let Z : Fin 3 × Fin 3 → ℤ := fun p => ((S p).val : ℤ) + 1 - 5
The centered matrix Z has zero column moment, zero row moment, zero diagonal and anti-diagonal sums, and zero total sum.
Expanding the sums over Fin 3 with Fin.sum_univ_three, the center-of-mass shift subtracts 5 × 9 = 45 from the row and column moments, leaving 0. The diagonal sums each lose 3 × 5 = 15, leaving 0. Finally, arrangement3_sum_entries S gives the total sum of all nine entries as 45, so the total sum of Z is 45 - 9 × 5 = 0.
uses
Fin.sum_univ_three,arrangement3_sum_entrieshave hZcol : ∑ i : Fin 3, ∑ j : Fin 3, (j : ℤ) * Z (i,j) = 0 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, (j : ℤ) * Z (i,j)) = (∑ i : Fin 3, ∑ j : Fin 3, (j : ℤ) * (((S (i,j)).val : ℤ) + 1)) - 45 := by simp only [Z, Fin.sum_univ_three]; norm_num; ring rw [key, hcol]; ring have hZrow : ∑ i : Fin 3, ∑ j : Fin 3, (i : ℤ) * Z (i,j) = 0 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, (i : ℤ) * Z (i,j)) = (∑ i : Fin 3, ∑ j : Fin 3, (i : ℤ) * (((S (i,j)).val : ℤ) + 1)) - 45 := by simp only [Z, Fin.sum_univ_three]; norm_num; ring rw [key, hrow]; ring have hZdiag : ∑ i : Fin 3, Z (i,i) = 0 := by have key : (∑ i : Fin 3, Z (i,i)) = (∑ i : Fin 3, (((S (i,i)).val : ℤ) + 1)) - 15 := by simp only [Z, Fin.sum_univ_three]; ring rw [key, hdiag]; ring have hZanti : ∑ i : Fin 3, Z (i,i.rev) = 0 := by have key : (∑ i : Fin 3, Z (i,i.rev)) = (∑ i : Fin 3, (((S (i,i.rev)).val : ℤ) + 1)) - 15 := by simp only [Z, Fin.sum_univ_three]; ring rw [key, hanti]; ring have hZsum : ∑ i : Fin 3, ∑ j : Fin 3, Z (i,j) = 0 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, Z (i,j)) = (∑ i : Fin 3, ∑ j : Fin 3, (((S (i,j)).val : ℤ) + 1)) - 45 := by simp only [Z, Fin.sum_univ_three]; ring rw [key, arrangement3_sum_entries S]; ring
The sum of squares of all entries of Z is 60.
Expanding Z(p)² = (S(p)+1)² - 10(S(p)+1) + 25, we express the sum over the 3×3 grid as a sum over Fin 3 × Fin 3 via Fintype.sum_prod_type and reindex along the bijection S via Equiv.sum_comp. The sum of squares of 1 through 9 evaluates by decide to 285. Combining this with the total sum 45 from arrangement3_sum_entries S yields 285 - 10 × 45 + 9 × 25 = 60.
uses
Fin.sum_univ_three,Fintype.sum_prod_type,Equiv.sum_comp,arrangement3_sum_entrieshave hZsq : ∑ i : Fin 3, ∑ j : Fin 3, Z (i,j)^2 = 60 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, Z (i,j)^2) = (∑ i : Fin 3, ∑ j : Fin 3, ((((S (i,j)).val : ℤ) + 1)^2)) - 10 * (∑ i : Fin 3, ∑ j : Fin 3, (((S (i,j)).val : ℤ) + 1)) + 225 := by simp only [Z, Fin.sum_univ_three]; ring rw [key] have hsq : (∑ i : Fin 3, ∑ j : Fin 3, ((((S (i,j)).val : ℤ) + 1) ^ 2)) = 285 := by rw [← Fintype.sum_prod_type (fun p : Fin 3 × Fin 3 => ((((S p).val : ℤ) + 1)^2))] rw [Equiv.sum_comp S (fun k : Fin 9 => (((k : ℤ) + 1)^2))] decide rw [hsq, arrangement3_sum_entries S]; ring
The summations on Z expand to the exact scalar hypotheses needed by the core lemma.
Using Fin.sum_univ_three, the summations over Fin 3 expand into explicit coordinate-wise equalities on the values Z(i, j), evaluating the reversed indices Fin.rev 0, 1, 2 to 2, 1, 0.
uses
Fin.sum_univ_threehave hsumZ' : Z (0,0)+Z (0,1)+Z (0,2)+Z (1,0)+Z (1,1)+Z (1,2)+Z (2,0)+Z (2,1)+Z (2,2) = 0 := by have hh := hZsum; simp only [Fin.sum_univ_three] at hh; linarith have hcolZ' : Z (0,1) + 2*Z (0,2) + Z (1,1) + 2*Z (1,2) + Z (2,1) + 2*Z (2,2) = 0 := by have hh := hZcol; simp only [Fin.sum_univ_three] at hh; norm_num at hh; linarith have hrowZ' : Z (1,0)+Z (1,1)+Z (1,2) + 2*(Z (2,0)+Z (2,1)+Z (2,2)) = 0 := by have hh := hZrow; simp only [Fin.sum_univ_three] at hh; norm_num at hh; linarith have hdiagZ' : Z (0,0)+Z (1,1)+Z (2,2) = 0 := by have hh := hZdiag; simp only [Fin.sum_univ_three] at hh; linarith have hantiZ' : Z (0,2)+Z (1,1)+Z (2,0) = 0 := by have hh := hZanti; simp only [Fin.sum_univ_three] at hh rw [show (Fin.rev (0:Fin 3)) = (2:Fin 3) from by decide, show (Fin.rev (1:Fin 3)) = (1:Fin 3) from by decide, show (Fin.rev (2:Fin 3)) = (0:Fin 3) from by decide] at hh linarith have hsqZ' : Z (0,0)^2+Z (0,1)^2+Z (0,2)^2+Z (1,0)^2+Z (1,1)^2+Z (1,2)^2+Z (2,0)^2+Z (2,1)^2+Z (2,2)^2 = 60 := by have hh := hZsq; simp only [Fin.sum_univ_three] at hh; linarith
The centered function Z is injective, so its values at distinct corner positions are distinct.
If Z(p) = Z(q), then S(p).val = S(q).val, so S(p) = S(q) by Fin.ext, and since S is a bijection, p = q. Thus Z is injective, which immediately provides the four required distinctness hypotheses Z(0,0) ≠ Z(2,2), Z(0,2) ≠ Z(2,0), Z(0,0) ≠ Z(0,2), and Z(0,2) ≠ Z(2,2).
uses
Fin.exthave hZinj : Function.Injective Z := by intro p q hpq simp only [Z] at hpq have hval : (S p).val = (S q).val := by omega have : S p = S q := Fin.ext hval exact S.injective this have hne1 : Z (0,0) ≠ Z (2,2) := by intro hh; have := hZinj hh; simp at this have hne2 : Z (0,2) ≠ Z (2,0) := by intro hh; have := hZinj hh; simp at this have hne3 : Z (0,0) ≠ Z (0,2) := by intro hh; have := hZinj hh; simp at this have hne4 : Z (0,2) ≠ Z (2,2) := by intro hh; have := hZinj hh; simp at this
All three row sums and all three column sums of Z are zero.
We apply arrangement3_e4_zero_iff_is_magic_core to the nine entries of Z, using the verified total sum, moments, diagonal sums, sum of squares 60, and corner distinctness properties.
uses
arrangement3_e4_zero_iff_is_magic_coreobtain ⟨hR0, hR1, hR2, hD0, hD1, hD2⟩ := arrangement3_e4_zero_iff_is_magic_core (Z (0,0)) (Z (0,1)) (Z (0,2)) (Z (1,0)) (Z (1,1)) (Z (1,2)) (Z (2,0)) (Z (2,1)) (Z (2,2)) hsumZ' hcolZ' hrowZ' hdiagZ' hantiZ' hsqZ' hne1 hne2 hne3 hne4S is a magic square: all row and column sums of S(i, j) + 1 equal 15, and diagonal sums equal 15.
Unfolding is_magic_arrangement3, the diagonal sums equal 15 directly from the energy-zero hypothesis. For each row i and column j, exhaustive case analysis with fin_cases expands the sums with Fin.sum_univ_three; since the corresponding row or column sum of Z is 0, adding 3 × 5 = 15 shows the sum of S entries is 15.
uses
is_magic_arrangement3,Fin.sum_univ_threerw [is_magic_arrangement3] refine ⟨?_, ?_, hdiag, hanti⟩ · intro i fin_cases i · beta_reduce change (∑ j : Fin 3, (((S (0,j)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hR0; linarith · beta_reduce change (∑ j : Fin 3, (((S (1,j)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hR1; linarith · beta_reduce change (∑ j : Fin 3, (((S (2,j)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hR2; linarith · intro j fin_cases j · beta_reduce change (∑ i : Fin 3, (((S (i,0)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hD0; linarith · beta_reduce change (∑ i : Fin 3, (((S (i,1)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hD1; linarith · beta_reduce change (∑ i : Fin 3, (((S (i,2)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hD2; linarith
Conversely, any magic square of order 3 satisfies E₄(S) = 0.
The reverse implication is provided directly by the existing lemma arrangement3_e4_zero_of_is_magic.
uses
arrangement3_e4_zero_of_is_magic· exact arrangement3_e4_zero_of_is_magic S
mul_eq_zero.mp- If the product of two numbers is zero, at least one of the factors is zero.
arrangement3_diophantine_system- For integers κ, a', c', ξ', η', if ξ' + η' = κ, 4κ² + 3(a'² + c'²) + ξ'² + η'² = 30, and a' ≠ 0, c' ≠ 0, a'² ≠ c'², then κ = 0, ξ' = 0, and η' = 0.
Fin.sum_univ_three- The sum of a function over Fin 3 equals the sum of its values at 0, 1, and 2.
arrangement3_sum_entries- The sum of all nine entries (shifted to 1..9) in an order 3 arrangement S is 45.
Fintype.sum_prod_type- A sum over pairs in a product of two finite types equals the iterated sum over the components.
Equiv.sum_comp- Summing a function reindexed by a bijection (equivalence) between finite types preserves the sum.
Fin.ext- Two elements of Fin n are equal if and only if their underlying natural values are equal.
arrangement3_e4_zero_iff_is_magic_core- A 3×3 integer matrix with zero total sum, zero first moments, zero diagonal sums, sum of squares 60, and distinct corners has all row and column sums equal to zero.
is_magic_arrangement3- An arrangement of order 3 is a magic square if all row sums, all column sums, and both main diagonal sums equal 15.
arrangement3_e4_zero_of_is_magic- Every magic square arrangement of order 3 satisfies the four-term line-sum energy zero condition E₄(S) = 0.
- The proof centers the entries from 1..9 to -4..4 by defining Z(p) = (S(p) + 1) - 5, which shifts the total sum to 0 and simplifies the line moments.
- The sum of squares of the integers from 1 to 9 (evaluating to 285) is computed automatically by kernel evaluation (`decide`).
Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.
Lean source view module on GitHub
Choose a tactic — a marked line — to see the goals Lean had before and after it, as in Lean’s infoview. Step with ← → or j k.
private lemma arrangement3_e4_zero_iff_is_magic_core (p00 p01 p02 p10 p11 p12 p20 p21 p22 : ℤ) (hsum : p00+p01+p02+p10+p11+p12+p20+p21+p22 = 0) (hcol : p01 + 2*p02 + p11 + 2*p12 + p21 + 2*p22 = 0) (hrow : p10+p11+p12+2*(p20+p21+p22) = 0) (hdiag : p00+p11+p22=0) (hanti : p02+p11+p20=0) (hsq : p00^2+p01^2+p02^2+p10^2+p11^2+p12^2+p20^2+p21^2+p22^2 = 60) (hne1 : p00 ≠ p22) (hne2 : p02 ≠ p20) (hne3 : p00 ≠ p02) (hne4 : p02 ≠ p22) : p00+p01+p02 = 0 ∧ p10+p11+p12=0 ∧ p20+p21+p22=0 ∧ p00+p10+p20=0 ∧ p01+p11+p21=0 ∧ p02+p12+p22=0 := by set ξ := p00+p10+p20 with hξ set η := p00+p01+p02 with hη have hRel : 3*p11 = -2*(ξ+η) := by linarith have hdvd : 2 ∣ p11 := by omega obtain ⟨μ, hμ⟩ := hdvd have hκ : p11 = -2*(-μ) := by linarith set κ := -μ with hκdef have hξη : ξ + η = 3*κ := by linarith have hκA : 2*κ = p00 + p22 := by linarith have hκB : 2*κ = p02 + p20 := by linarith have h22 : p22 = -p00 - p11 := by linarith have h20 : p20 = -p02 - p11 := by linarith have h01 : p01 = η - p00 - p02 := by linarith have h10 : p10 = ξ - p00 - p20 := by linarith have h12 : p12 = ξ - p02 - p22 := by linarith have h21 : p21 = -2*ξ - p11 - p01 := by linarith have hη' : η = 3*κ - ξ := by linarith have heq : 4*κ^2+3*((p00-κ)^2+(p02-κ)^2)+(ξ-κ)^2+(η-κ)^2 = 30 := by have hident : p00^2+p01^2+p02^2+p10^2+p11^2+p12^2+p20^2+p21^2+p22^2 = 2*(4*κ^2+3*((p00-κ)^2+(p02-κ)^2)+(ξ-κ)^2+(η-κ)^2) := by simp only [h22, h20, h01, h12, h21, h10, hκ, hη']; ring linarith [hsq, hident] have hc1 : p00 - κ ≠ 0 := by intro h; apply hne1; linarith have hc2 : p02 - κ ≠ 0 := by intro h; apply hne2; linarith have hc3 : (p00-κ)^2 ≠ (p02-κ)^2 := by intro hh have hprod : ((p00-κ)-(p02-κ))*((p00-κ)+(p02-κ)) = 0 := by nlinarith [hh] have hsub : (p00-κ)-(p02-κ) ≠ 0 := by intro h; apply hne3; linarith have hadd : (p00-κ)+(p02-κ) ≠ 0 := by intro h; apply hne4; linarith rcases mul_eq_zero.mp hprod with h | h · exact hsub h · exact hadd h obtain ⟨hκ0, hξ', hη'0⟩ := arrangement3_diophantine_system κ (p00-κ) (p02-κ) (ξ-κ) (η-κ) (by linarith) heq ⟨hc1, hc2, hc3⟩ have hξ0 : ξ = 0 := by linarith have hη0 : η = 0 := by linarith refine ⟨by linarith, by linarith, by linarith, by linarith, by linarith, by linarith⟩/-- Proposition 5.1: an arrangement S of order 3 has E₄(S) = 0 if and only if S is a magic square. -/theorem arrangement3_e4_zero_iff_is_magic (S : Fin 3 × Fin 3 ≃ Fin 9) : arrangement3_e4_zero S ↔ is_magic_arrangement3 S := by constructor · intro h obtain ⟨hcol, hrow, hdiag, hanti⟩ := h let Z : Fin 3 × Fin 3 → ℤ := fun p => ((S p).val : ℤ) + 1 - 5 have hZcol : ∑ i : Fin 3, ∑ j : Fin 3, (j : ℤ) * Z (i,j) = 0 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, (j : ℤ) * Z (i,j)) = (∑ i : Fin 3, ∑ j : Fin 3, (j : ℤ) * (((S (i,j)).val : ℤ) + 1)) - 45 := by simp only [Z, Fin.sum_univ_three]; norm_num; ring rw [key, hcol]; ring have hZrow : ∑ i : Fin 3, ∑ j : Fin 3, (i : ℤ) * Z (i,j) = 0 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, (i : ℤ) * Z (i,j)) = (∑ i : Fin 3, ∑ j : Fin 3, (i : ℤ) * (((S (i,j)).val : ℤ) + 1)) - 45 := by simp only [Z, Fin.sum_univ_three]; norm_num; ring rw [key, hrow]; ring have hZdiag : ∑ i : Fin 3, Z (i,i) = 0 := by have key : (∑ i : Fin 3, Z (i,i)) = (∑ i : Fin 3, (((S (i,i)).val : ℤ) + 1)) - 15 := by simp only [Z, Fin.sum_univ_three]; ring rw [key, hdiag]; ring have hZanti : ∑ i : Fin 3, Z (i,i.rev) = 0 := by have key : (∑ i : Fin 3, Z (i,i.rev)) = (∑ i : Fin 3, (((S (i,i.rev)).val : ℤ) + 1)) - 15 := by simp only [Z, Fin.sum_univ_three]; ring rw [key, hanti]; ring have hZsum : ∑ i : Fin 3, ∑ j : Fin 3, Z (i,j) = 0 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, Z (i,j)) = (∑ i : Fin 3, ∑ j : Fin 3, (((S (i,j)).val : ℤ) + 1)) - 45 := by simp only [Z, Fin.sum_univ_three]; ring rw [key, arrangement3_sum_entries S]; ring have hZsq : ∑ i : Fin 3, ∑ j : Fin 3, Z (i,j)^2 = 60 := by have key : (∑ i : Fin 3, ∑ j : Fin 3, Z (i,j)^2) = (∑ i : Fin 3, ∑ j : Fin 3, ((((S (i,j)).val : ℤ) + 1)^2)) - 10 * (∑ i : Fin 3, ∑ j : Fin 3, (((S (i,j)).val : ℤ) + 1)) + 225 := by simp only [Z, Fin.sum_univ_three]; ring rw [key] have hsq : (∑ i : Fin 3, ∑ j : Fin 3, ((((S (i,j)).val : ℤ) + 1) ^ 2)) = 285 := by rw [← Fintype.sum_prod_type (fun p : Fin 3 × Fin 3 => ((((S p).val : ℤ) + 1)^2))] rw [Equiv.sum_comp S (fun k : Fin 9 => (((k : ℤ) + 1)^2))] decide rw [hsq, arrangement3_sum_entries S]; ring have hsumZ' : Z (0,0)+Z (0,1)+Z (0,2)+Z (1,0)+Z (1,1)+Z (1,2)+Z (2,0)+Z (2,1)+Z (2,2) = 0 := by have hh := hZsum; simp only [Fin.sum_univ_three] at hh; linarith have hcolZ' : Z (0,1) + 2*Z (0,2) + Z (1,1) + 2*Z (1,2) + Z (2,1) + 2*Z (2,2) = 0 := by have hh := hZcol; simp only [Fin.sum_univ_three] at hh; norm_num at hh; linarith have hrowZ' : Z (1,0)+Z (1,1)+Z (1,2) + 2*(Z (2,0)+Z (2,1)+Z (2,2)) = 0 := by have hh := hZrow; simp only [Fin.sum_univ_three] at hh; norm_num at hh; linarith have hdiagZ' : Z (0,0)+Z (1,1)+Z (2,2) = 0 := by have hh := hZdiag; simp only [Fin.sum_univ_three] at hh; linarith have hantiZ' : Z (0,2)+Z (1,1)+Z (2,0) = 0 := by have hh := hZanti; simp only [Fin.sum_univ_three] at hh rw [show (Fin.rev (0:Fin 3)) = (2:Fin 3) from by decide, show (Fin.rev (1:Fin 3)) = (1:Fin 3) from by decide, show (Fin.rev (2:Fin 3)) = (0:Fin 3) from by decide] at hh linarith have hsqZ' : Z (0,0)^2+Z (0,1)^2+Z (0,2)^2+Z (1,0)^2+Z (1,1)^2+Z (1,2)^2+Z (2,0)^2+Z (2,1)^2+Z (2,2)^2 = 60 := by have hh := hZsq; simp only [Fin.sum_univ_three] at hh; linarith have hZinj : Function.Injective Z := by intro p q hpq simp only [Z] at hpq have hval : (S p).val = (S q).val := by omega have : S p = S q := Fin.ext hval exact S.injective this have hne1 : Z (0,0) ≠ Z (2,2) := by intro hh; have := hZinj hh; simp at this have hne2 : Z (0,2) ≠ Z (2,0) := by intro hh; have := hZinj hh; simp at this have hne3 : Z (0,0) ≠ Z (0,2) := by intro hh; have := hZinj hh; simp at this have hne4 : Z (0,2) ≠ Z (2,2) := by intro hh; have := hZinj hh; simp at this obtain ⟨hR0, hR1, hR2, hD0, hD1, hD2⟩ := arrangement3_e4_zero_iff_is_magic_core (Z (0,0)) (Z (0,1)) (Z (0,2)) (Z (1,0)) (Z (1,1)) (Z (1,2)) (Z (2,0)) (Z (2,1)) (Z (2,2)) hsumZ' hcolZ' hrowZ' hdiagZ' hantiZ' hsqZ' hne1 hne2 hne3 hne4 rw [is_magic_arrangement3] refine ⟨?_, ?_, hdiag, hanti⟩ · intro i fin_cases i · beta_reduce change (∑ j : Fin 3, (((S (0,j)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hR0; linarith · beta_reduce change (∑ j : Fin 3, (((S (1,j)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hR1; linarith · beta_reduce change (∑ j : Fin 3, (((S (2,j)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hR2; linarith · intro j fin_cases j · beta_reduce change (∑ i : Fin 3, (((S (i,0)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hD0; linarith · beta_reduce change (∑ i : Fin 3, (((S (i,1)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hD1; linarith · beta_reduce change (∑ i : Fin 3, (((S (i,2)).val : ℤ) + 1)) = 15 simp only [Fin.sum_univ_three]; simp only [Z] at hD2; linarith · exact arrangement3_e4_zero_of_is_magic S