Counting magic squares — arrays of nonnegative integers whose rows, columns and two main diagonals all share a common line sum — is one of the oldest problems in enumerative combinatorics, and the testing ground on which the general theory was built. MacMahon computed the order-three count in 1915 by hand; sixty years later Stanley, and then Beck, Cohen, Cuomo and Gribelyuk (Amer. Math. Monthly 110 (2003), 707--717), showed that for general order the counting functions are quasi-polynomials in the line sum, by identifying them with Ehrhart quasi-polynomials of rational polytopes. The order-three case is the oldest nontrivial instance of that theory and the one where every step can still be checked by hand.
The subject therefore has a curious status: the enumerative answer for has been known for over a century, and the structural facts behind it (a magic square is determined by two corner entries; opposite cells sum to twice the centre) are folklore — but none of it has a machine-checked proof. This mission formalizes the classical derivation end to end.
Fix an order and a type of entries. A square of order is an array with entries in ; its row sums, column sums, and the two diagonal sums (main and anti-diagonal) are the sums of the entries along those lines.
No distinctness of entries is required. Let denote the number of semi-magic and the number of magic squares of order with nonnegative integer entries and line sum . Every entry of such a square is at most , so these are finite counts.
For the whole family is parametrized. If has line sum then the centre cell equals , and writing and the eight line identities force
All nine entries are nonnegative exactly when
and substituting , turns these into : the ball of radius in .
together with the companion vanishing when . This is the count of magic squares of line sum with nonnegative integer entries (entries need not be distinct). It is the goal because it is the weakest stable statement: it asserts only the shape of the answer, not the intermediate parametrization, and it survives verbatim as the case of the general quasi-polynomial theorem.
That the map is a bijection from the magic squares of line sum onto the admissible parameter pairs, and that the latter are counted by the -ball cardinality. This is the route the mission actually takes; the count is its corollary.
, the analogous count for semi-magic squares, is a genuinely different and harder quasi-polynomial. It is listed as a stretch target, not a milestone.
The result itself. MacMahon's formula is the base case of the Ehrhart-theory reading of magic-square enumeration; Beck--Cohen--Cuomo--Gribelyuk's quasi-polynomial theorem for general degenerates to it at , so it is the sanity check any generalization must pass. The parametrization behind it is what makes the "how many" question finite-dimensional at all: it reduces a search over arrays to a count of lattice points in a two-dimensional ball. Downstream, the same parametrization governs the classification of normal magic squares (the Lo Shu square and its symmetries) and the associativity identity .
Formalizing it. The mathematics is classical and proved; nothing here is
open. What is missing is the formalized artifact. The order-three structural
lemmas — the centre identity, the opposite-cell identity, and the two directions
of the parametrization — are already machine-checked on this platform. The
remaining work is the counting step: exhibiting a concrete bijection between
two finsets whose elements live in different types (arrays over Fin (3e+1)
versus pairs of naturals) and evaluating a finite sum. That is where the
formalization, not the mathematics, is hard.
The obvious attack — "each magic square is determined by , so just count
the pairs" — fails at exactly one point, and it is not a mathematical point. The
counting function is defined as the cardinality of a finset of arrays with
entries in Fin (3e+1) (a finite type, so that Finset.univ exists), whereas
the parametrization lives over . Proving the counts agree therefore
requires a honest Finset.card_bij in both directions:
mkMagic3 from an admissible pair, coerce every entry into
Fin (3e+1) using the bound , and show the round trip is
the identity.Neither direction is deep, but the coercions are unforgiving: a truncated
subtraction in mkMagic3 is only correct because admissibility forbids the
truncation, and that side condition must be discharged explicitly rather than
assumed. The second difficulty is the cardinality of the ball: the
identification needs the elementary identity
, after which the count is
.
Fin n; the anti-diagonal uses Fin.rev, and broken
diagonals use addition modulo . Counting functions are cardinalities of
finsets of arrays over Fin (t+1) — lossless, since every entry is at most
— and return natural numbers.mkMagic3 is defined over with truncated subtraction. Every
row/column/diagonal identity therefore carries the admissibility inequalities
as explicit hypotheses; no identity is asserted unconditionally.Square, IsSemiMagic,
IsMagic, IsPanMagic, IsAssociative, IsNormal, magicConstant, and the
four counting functions ), the symmetry/affine toolbox, and
the order-three structural lemmas. Contributions are welcome on the
semi-magic count , on panmagic and associative refinements, and on the
extension to general .namespace MagicSquares
theorem magic_count_three_divisible (e : ℕ) :
magicCount 3 (3 * e) = 2 * e ^ 2 + 2 * e + 1 := by sorry
end MagicSquaresMacMahon's 1915 count of magic squares whose line sum is a multiple of .
Write for the number of arrays of nonnegative integers whose three rows, three columns and two main diagonals all sum to (entries need not be distinct). Then vanishes unless , and for ,
which is the integral form of .
The companion statement MagicSquares.magic_count_three_otherwise records the vanishing when
; together they give the complete counting function.
Formalization Note magicCount n t counts arrays with entries in Fin (t+1) satisfying
the magic identities after coercion to ℕ. This is lossless because every entry of a
nonnegative magic square with line sum is at most .
No open leaves. Every sub-goal is proved or awaiting decomposition.