Skip to content

Instantly share code, notes, and snippets.

@gideongrinberg
Last active May 18, 2025 20:54
Show Gist options
  • Save gideongrinberg/ccb97ee98d2a2c0e92dde61dddb3553b to your computer and use it in GitHub Desktop.
Save gideongrinberg/ccb97ee98d2a2c0e92dde61dddb3553b to your computer and use it in GitHub Desktop.
Magic Square Proof Attempt
-- This module serves as the root of the `MagicSquare` library.
-- Import modules here that should be built as part of the library.
import Mathlib.Data.ZMod.Basic
import Mathlib.Data.Int.Basic
import Mathlib.Tactic.Ring.RingNF
import Mathlib.Algebra.Group.Defs
import Mathlib.Tactic.LinearCombination
import Mathlib.Tactic.Linarith.Frontend
import Mathlib.Data.Complex.Basic
import Mathlib.Data.Complex.Norm
import Mathlib.Analysis.Complex.Arg
import Mathlib.Data.Real.Sqrt
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Complex
import MagicSquare.Lemmas
open MagicSquare.Lemmas (angle_mod_basic)
-- Gaussian Integer logic
structure GaussianInt where
re : ℤ -- Real part
im : ℤ -- Imaginary part
deriving DecidableEq, Repr
instance : CoeSort GaussianInt (ℤ × ℤ) where
coe := fun ⟨a, b⟩ => (a, b)
instance : Add GaussianInt where
add a b := ⟨a.re + b.re, a.im + b.im⟩
instance : Mul GaussianInt where
mul a b := ⟨a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re⟩
instance : Neg GaussianInt where
neg a := ⟨-a.re, -a.im⟩
def GaussianInt.norm (α : GaussianInt) : ℤ :=
α.re ^ 2 + α.im ^ 2
def GaussianInt.conj (a : GaussianInt) : GaussianInt := ⟨a.re, -a.im⟩
lemma GaussianInt.norm_nonneg (α : GaussianInt) : 0 ≤ α.norm := by
cases' α with a b
have h₁ : 0 ≤ a ^ 2 := by apply sq_nonneg
have h₂ : 0 ≤ b ^ 2 := by apply sq_nonneg
exact add_nonneg h₁ h₂
instance : HPow GaussianInt ℕ GaussianInt where
hPow := fun a n =>
Nat.recOn n
(⟨1, 0⟩ : GaussianInt)
(fun _ ih => a * ih)
@[simp]
lemma hPow_zero (a : GaussianInt) : a ^ (0 : ℕ) = ⟨1, 0⟩ := by
-- `Nat.recOn 0 _ _` reduces directly to the base case
rfl
@[simp]
lemma hPow_succ (a : GaussianInt) (n : ℕ) :
a ^ (n + 1) = a * a ^ n := by
-- `Nat.recOn (n+1) _ _` reduces to the “step” case
rfl
@[simp] lemma mul_re (a b : GaussianInt) : (a * b).re = a.re * b.re - a.im * b.im := rfl
@[simp] lemma mul_im (a b : GaussianInt) : (a * b).im = a.re * b.im + a.im * b.re := rfl
-- Basic norm properties
theorem gnorm_mul (α β : GaussianInt) :
(α * β).norm = α.norm * β.norm := by
-- Expand definitions and simplify using algebraic properties
simp [GaussianInt.norm, mul_re, mul_im, sq]
ring -- Simplify the expression using ring operations
lemma norm_eq_sq_mag (α : GaussianInt) :
α.norm = (α.re ^ 2 + α.im ^ 2) := rfl
lemma sq_eq_sq_of_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) (h : a ^ 2 = b ^ 2) : a = b := by
nlinarith
-- Check if pairs p and q are distinct
-- modulo order and sign
def pairwiseDistinct (p q : ℤ × ℤ) : Prop :=
let (a, b) := p
let (c, d) := q
¬ ((a = c ∧ b = d) ∨ -- identity
(a = d ∧ b = c) ∨ -- swap
(a = -c ∧ b = -d) ∨ -- negate both
(a = -d ∧ b = -c) ∨ -- swap + negate both
(a = c ∧ b = -d) ∨ -- negate second
(a = -c ∧ b = d) ∨ -- negate first
(a = -d ∧ b = c) ∨ -- swap + negate first
(a = d ∧ b = -c)) -- swap + negate second
def pairwiseEquivalent (p q : GaussianInt) : Prop :=
¬ pairwiseDistinct p q
theorem main_theorem : ¬(∃ (m n p q u v : ℤ),
(m^2 + n^2)^2 = (p^2 + q^2)^2 ∧ (p^2 + q^2)^2 = (u^2 + v^2)^2 ∧
4 * m * n * (m^2 - n^2) + 4 * p * q * (p^2 - q^2) + 4 * u * v * (u^2 - v^2) = 0 ∧
pairwiseDistinct (m, n) (p, q) ∧
pairwiseDistinct (p, q) (u, v) ∧
pairwiseDistinct (m, n) (u, v)
):= by
rintro ⟨m, n, p, q, r, s, h₁, h₂, h₃, h₄, h₅, h₆⟩
-- Translate into ℤ[i]
let α : GaussianInt := ⟨m, n⟩
let β : GaussianInt := ⟨p, q⟩
let γ : GaussianInt := ⟨r, s⟩
have h₁' : α.norm = β.norm := by
apply sq_eq_sq_of_nonneg (GaussianInt.norm_nonneg α) (GaussianInt.norm_nonneg β)
exact h₁
have h₂' : β.norm = γ.norm := by
apply sq_eq_sq_of_nonneg (GaussianInt.norm_nonneg β) (GaussianInt.norm_nonneg γ)
exact h₂
have h_norm : α.norm = β.norm ∧ β.norm = γ.norm := ⟨h₁', h₂'⟩
have h_imag : (α^4).im + (β^4).im + (γ^4).im = 0 := by
simp [hPow_succ, mul_im, mul_re, h₃]
linear_combination h₃
have h_norm_val : α.norm = β.norm := h_norm.1
have h_norm_val' : β.norm = γ.norm := h_norm.2
let t := α.norm
-- Prove that α β and γ are on the unit circle
let α_unit : ℂ := ⟨α.re / Real.sqrt t, α.im / Real.sqrt t⟩
let β_unit : ℂ := ⟨β.re / Real.sqrt t, β.im / Real.sqrt t⟩
let γ_unit : ℂ := ⟨γ.re / Real.sqrt t, γ.im / Real.sqrt t⟩
have h_unit_norm : ‖α_unit‖ = 1 ∧ ‖β_unit‖ = 1 ∧ ‖γ_unit‖ = 1 := by
have ht : (0 : ℝ) < t := by
sorry
apply And.intro
. -- proof of α
sorry
. apply And.intro
. -- proof of β
sorry
. -- proof of γ
sorry
-- Define angles from normalized complex numbers
let θ_α := Complex.arg α_unit
let θ_β := Complex.arg β_unit
let θ_γ := Complex.arg γ_unit
-- Express imaginary parts using sine
have h_sin_sum : Real.sin (4 * θ_α) + Real.sin (4 * θ_β) + Real.sin (4 * θ_γ) = 0 := by
exact sorry
have h_angle_symm : ∃ (φ ψ : ℝ),
Real.sin φ + Real.sin ψ + Real.sin (-(φ + ψ)) = 0 := by
sorry
obtain ⟨φ, ψ, hφψ⟩ := h_angle_symm
-- Conclude two angles must be negatives modulo 2π
-- have h_angle_mod :
-- (∃ k : ℤ, φ + ψ = (k : ℝ) * Real.pi) ∨
-- (∃ k : ℤ, φ = -ψ + (k : ℝ) * 2 * Real.pi) := by sorry
have h_angle_mod :
(∃ k : ℤ, φ + ψ = (k : ℝ) * Real.pi) ∨
(∃ k : ℤ, φ = (k : ℝ) * 2 * Real.pi) ∨
(∃ k : ℤ, ψ = (k : ℝ) * 2 * Real.pi) := by
sorry
-- Connect angles to Gaussian integer equivalence
have h_equiv : α = β.conj ∨ β = γ.conj ∨ γ = α.conj := by
sorry
rcases h_equiv with hαβ | hβγ | hγα
· -- α equals the conjugate of β → contradicts h₄
have h_re : m = p := by
simpa [α, β, GaussianInt.conj] using congrArg GaussianInt.re hαβ
have h_im : n = -q := by
simpa [α, β, GaussianInt.conj] using congrArg GaussianInt.im hαβ
have : False := by
-- after substituting `h_re`, `h_im`, the disjunction in `pairwiseDistinct`
-- is true, so `h₄ : ¬ True`, hence `h₄ trivial` is a contradiction
have hcontra : True → False := by
simp [pairwiseDistinct, h_re, h_im] at h₄
exact hcontra trivial
exact this
· -- β equals the conjugate of γ → contradicts h₅
have h_re : p = r := by
simpa [β, γ, GaussianInt.conj] using congrArg GaussianInt.re hβγ
have h_im : q = -s := by
simpa [β, γ, GaussianInt.conj] using congrArg GaussianInt.im hβγ
have : False := by
have hcontra : True → False := by
simp [pairwiseDistinct, h_re, h_im] at h₅
exact hcontra trivial
exact this
· -- γ equals the conjugate of α → contradicts h₆
have h_re : r = m := by
simpa [γ, α, GaussianInt.conj] using congrArg GaussianInt.re hγα
have h_im : s = -n := by
simpa [γ, α, GaussianInt.conj] using congrArg GaussianInt.im hγα
have : False := by
have hcontra : True → False := by
simp [pairwiseDistinct, h_re, h_im] at h₆
exact hcontra trivial
exact this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment