Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

MacMahon parametrization of 3x3 magic squares

Definition
MagicSquaresParam3

by Yuxuan Xu · Sep 16, 2026 · Mathlib 0df444a (Lean v4.33.1)

combinatoricsenumerative-combinatoricsmagic-squares

The MacMahon parametrization of 3×33 \times 33×3 magic squares.

Let MMM be a 3×33\times33×3 array of nonnegative integers whose three rows, three columns and two main diagonals all sum to s=3es = 3es=3e. MacMahon's centre identity gives M11=eM_{11} = eM11​=e, and then the eight line identities force

M  =  (a3e−a−cce+c−aee+a−c2e−ca+c−e2e−a),a=M00,  c=M02.M \;=\; \begin{pmatrix} a & 3e-a-c & c \\ e+c-a & e & e+a-c \\ 2e-c & a+c-e & 2e-a \end{pmatrix}, \qquad a = M_{00},\; c = M_{02}.M=​ae+c−a2e−c​3e−a−cea+c−e​ce+a−c2e−a​​,a=M00​,c=M02​.

So the square is determined by the pair (a,c)(a,c)(a,c), and all nine entries are nonnegative precisely when

e≤a+c≤3e,a≤e+c,c≤e+a.e \le a + c \le 3e, \qquad a \le e + c, \qquad c \le e + a .e≤a+c≤3e,a≤e+c,c≤e+a.

These inequalities imply 0≤a,c≤2e0 \le a, c \le 2e0≤a,c≤2e: adding a+c≤3ea + c \le 3ea+c≤3e to a≤e+ca \le e + ca≤e+c yields 2a≤4e2a \le 4e2a≤4e, and symmetrically for ccc. Substituting p=a−ep = a - ep=a−e, q=c−eq = c - eq=c−e, the four inequalities become ∣p+q∣≤e|p+q| \le e∣p+q∣≤e and ∣p−q∣≤e|p-q| \le e∣p−q∣≤e, equivalently ∣p∣+∣q∣≤e|p| + |q| \le e∣p∣+∣q∣≤e; hence the admissible pairs are in bijection with the ℓ1\ell_{1}ℓ1​ ball of radius eee in Z2\mathbb{Z}^{2}Z2, which has

1+4∑k=1ek  =  2e2+2e+11 + 4\sum_{k=1}^{e} k \;=\; 2e^{2} + 2e + 11+4k=1∑e​k=2e2+2e+1

lattice points. This is MacMahon's count M3(3e)=2e2+2e+1M_{3}(3e) = 2e^{2} + 2e + 1M3​(3e)=2e2+2e+1.

Formalization Note mkMagic3 e a c builds the array above using truncated natural-number subtraction; the row/column/diagonal identities only hold under the admissibility inequalities, which are collected in IsParam3 e a c. paramSet e is the finite set of admissible pairs, searched inside the lossless box [0,2e]×[0,2e][0,2e] \times [0,2e][0,2e]×[0,2e], and paramCount e is its cardinality. Both paramSet and paramCount are noncomputable because membership in paramSet involves a decidable proposition.

Definition code
import Mathlib
import Definitions.Def_MagicSquares

set_option autoImplicit false

/-!
# Magic squares of order three: the MacMahon parametrization

This module sets up the classical parametrization of $3 \times 3$ magic squares
that underlies MacMahon's count
$$M_{3}(3e) = 2e^{2} + 2e + 1 .$$

Let $M$ be a $3 \times 3$ magic square with nonnegative integer entries and line
sum $s = 3e$. By `center_of_order_three` the centre is $e$. Putting
$a = M_{00}$ and $c = M_{02}$ and chasing the eight line identities gives

$$M \;=\; \begin{pmatrix}
a & 3e-a-c & c \\
e+c-a & e & e+a-c \\
2e-c & a+c-e & 2e-a
\end{pmatrix}$$

so the square is completely determined by the pair $(a, c)$. All nine entries are
nonnegative exactly when

$$e \le a + c \le 3e, \qquad a \le e + c, \qquad c \le e + a,$$

and these inequalities in turn force $0 \le a, c \le 2e$ (add $a + c \le 3e$ to
$a \le e + c$ to get $2a \le 4e$, and symmetrically). Writing $p = a - e$ and
$q = c - e$, the four inequalities say $|p+q| \le e$ and $|p-q| \le e$, i.e.
$|p| + |q| \le e$; so the parameter set is in bijection with the $\ell_{1}$ ball
of radius $e$ in $\mathbb{Z}^{2}$, which contains
$1 + 4\sum_{k=1}^{e} k = 2e^{2} + 2e + 1$ lattice points.

The module provides the parametrization `mkMagic3`, the finite parameter set
`paramSet`, and the counting function `paramCount`. The two substantive
statements — that `mkMagic3` sets up a bijection onto the magic squares, and that
`paramCount e = 2e^2 + 2e + 1` — are submitted separately as theorems.
-/

namespace MagicSquares

/-- The canonical $3 \times 3$ array attached to a line sum `3 * e` and two
parameters `a`, `c`, as in the module docstring. Entries are natural numbers, so
the subtractions are truncated; the row/column/diagonal identities hold under the
admissibility inequalities recorded in `paramSet`. -/
def mkMagic3 (e a c : ℕ) : Square 3 ℕ :=
  ![![a, 3 * e - a - c, c],
    ![e + c - a, e, e + a - c],
    ![2 * e - c, a + c - e, 2 * e - a]]

noncomputable section

/-- Whether a parameter pair `(a, c)` is admissible for line sum `3 * e`. -/
def IsParam3 (e a c : ℕ) : Prop :=
  e ≤ a + c ∧ a + c ≤ 3 * e ∧ a ≤ e + c ∧ c ≤ e + a

/-- The finite set of admissible parameter pairs. As shown in the module
docstring, admissibility forces `a ≤ 2 * e` and `c ≤ 2 * e`, so searching the
`[0, 2e] × [0, 2e]` box is lossless. -/
def paramSet (e : ℕ) : Finset (ℕ × ℕ) :=
  by
    classical
    exact ((Finset.range (2 * e + 1)).product (Finset.range (2 * e + 1))).filter
      (fun ac => IsParam3 e ac.1 ac.2)

/-- The number of admissible parameter pairs for line sum `3 * e`. -/
def paramCount (e : ℕ) : ℕ := (paramSet e).card

end

end MagicSquares
Source
P. A. MacMahon, Combinatory Analysis (1915); see also G. Xin, Constructing all magic squares of order three, Electron. J. Combin. (2008), and Beck-Cohen-Cuomo-Gribelyuk, Amer. Math. Monthly 110 (2003).
Human review
  • Endorsed by Shuze Chen · Sep 16, 2026

  • Endorsed by Yuxuan Xu · Sep 16, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me