The magic-square programme already on this platform works at fixed small orders: MacMahon's enumeration of the squares, both the magic count and the semi-magic count ; the classification of the normal squares; and the counts of the panmagic and symmetric order-three classes. Each of those is a statement about a single order. This mission changes the axis: it asks what the counting function does when the order itself is allowed to vary.
Timeline.
That split is the point of the mission, so it is worth naming before anything is proved. A quasi-polynomial of degree and period agrees with a degree- polynomial on each residue class modulo , the polynomials differing between classes; a polynomial is the case . For the magic squares the values do depend on modulo a period — at order three vanishes unless — and the same is true of every other class. For it never happens.
An semi-magic square of line sum is an array of nonnegative integers in which every row and every column sums to . Entries may repeat, and no condition is placed on the diagonals. Write for the number of such arrays.
In Lean the array is a Square n ℕ, that is, a Matrix (Fin n) (Fin n) ℕ; the condition is IsSemiMagic M t, which asks every rowSum and every colSum to equal t; and the counting function is semiMagicCount n t, the cardinality of the finset of all arrays over Fin (t + 1) satisfying IsSemiMagic. Restricting the entries to Fin (t + 1) loses nothing, since an entry of a square of line sum is at most .
Dividing by turns such an array into a doubly stochastic matrix, a nonnegative real matrix whose every row and column sums to . So is equally the number of lattice points in the -fold dilation of the Birkhoff polytope . Two geometric facts about are what the mission is about. Its dimension is : the entries satisfy line equations, exactly one of which is dependent. Its vertices are the permutation matrices, by the Birkhoff–von Neumann theorem, hence integral. The mission states that both facts are visible in the arithmetic of .
This is Theorem 1 of [BCCG 2003], stated there for . It is the shape of the truth, not a closed form, so no later improvement of the explicit formulas can invalidate it. The three parts are not independent: the degree is the dimension of , and the two identities are the reciprocity law for lattice-point counting, applied to .
The goal is far from the easy cases, and the mission is laid out so that each rung is an independently provable statement.
MagicSquares.semi_magic_count_two () and MagicSquares.semi_magic_count_three (MacMahon's ). Included as references, not as targets.Its leading coefficient is and its normalised volume is .
The result itself. The theorem makes the semi-magic squares countable in closed form at every order, and it is why the semi-magic count can be tabulated as a polynomial while the magic, symmetric and pandiagonal counts cannot: a polynomial is determined by finitely many values, a quasi-polynomial is not without knowing its period. The reciprocity identities are the same statement seen from the interior of , which is why they are what pins an explicit polynomial down once its degree is known. The order-four polynomial above was verified against direct enumeration on seventeen values of ; that verification is evidence, not proof, and is recorded because the general statement is what has to be proved.
Formalizing it. The theorem has been known since 1973 and has had an elementary proof since 1980; what does not exist anywhere is a machine-checked proof. Mathlib contains no Ehrhart theory, no quasi-polynomial machinery and no rational-generating-function toolbox — the string "Ehrhart" does not occur in it — so a formalization must construct its own lattice-point-counting argument for this family of polytopes, or find an elementary route that avoids polytopes altogether. Either outcome is reusable: the same absence blocks the quasi-polynomial counts , and of BCCG's Theorem 2, which the earlier missions approach only at order three.
The first idea anyone has is to interpolate: compute for enough values of and fit a polynomial. That works, and it is how the order-four rung was produced, but it cannot prove the general statement: the degree is what is being asserted, so the number of values needed is not known in advance, and with itself a variable no finite computation settles it. Interpolation is legitimate as a target at order four; it must not be mistaken for a route to the goal.
The second idea is to import the geometry as a black box: a rational polytope dilated by has a counting function that is a quasi-polynomial of degree equal to its dimension, with period dividing the least common multiple of the vertex denominators. That is Ehrhart's theorem, and it is the textbook route. It is not available here, and reconstructing it in general is a larger project than this mission; the statements the mission asks for are the ones that survive without it.
The part of the goal with no counting interpretation at all is the second line. is defined on ; the assertion that a polynomial agreeing with it there vanishes at and satisfies is a statement about the interior of the polytope, and it cannot be read off from the combinatorial definition. A solver who proves only the polynomiality and the degree has not finished the goal.
The formalization commits to the following conventions.
semiMagicCount n t, the Finset.card of the arrays over Square n (Fin (t+1)) satisfying IsSemiMagic. Entries are natural numbers, not integers or reals.ℚ and is quantified existentially. Negative arguments are handled by casting the integer into ℚ and evaluating there; Polynomial.eval₂ is unusable for the reciprocity, since it would need a ring homomorphism , which does not exist.p.natDegree = (n - 1) ^ 2, with natural-number subtraction, which makes the case harmless rather than degenerate.1 ≤ n is carried explicitly although the statement is meaningful at ; it matches the source.∀ t by a finite range of values, or replacing semiMagicCount by a smooth surrogate, would make the statement easy and empty. The universal quantifier over and the exact value of natDegree are what give the goal its content.Reusable beyond this mission: any development of lattice-point counting in the Birkhoff polytope, of dilations of rational polytopes, or of quasi-polynomials. The vocabulary of the programme (MagicSquares, MagicSquaresPandiagonal, MagicSquaresMostPerfect, MagicSquaresTransforms, MagicSquaresNormal3) is shared with the earlier missions and is included as reference items rather than redefined.
namespace MagicSquares
theorem semi_magic_polynomial (n : ℕ) (hn : 1 ≤ n) :
∃ p : Polynomial ℚ,
p.natDegree = (n - 1) ^ 2 ∧
(∀ t : ℕ, p.eval (t : ℚ) = (semiMagicCount n t : ℚ)) ∧
(∀ t : ℤ, p.eval (((-(n : ℤ) - t : ℤ) : ℚ))
= (-1 : ℚ) ^ (n - 1) * p.eval ((t : ℤ) : ℚ)) ∧
(∀ k : ℤ, 1 ≤ k → k ≤ (n : ℤ) - 1 → p.eval (-(k : ℚ)) = 0) := by
sorry
end MagicSquaresGoal. For every order there is a polynomial which, simultaneously, has degree exactly , agrees with the counting function at every nonnegative integer, satisfies the reciprocity law for every integer , and vanishes at . This is Theorem 1 of Beck--Cohen--Cuomo--Gribelyuk, proved by Ehrhart and by Stanley in 1973 and given an elementary proof by Spencer in 1980. It separates the semi-magic squares from the magic, symmetric and pandiagonal classes, whose counting functions are quasi-polynomials rather than polynomials.
No open leaves. Every sub-goal is proved or awaiting decomposition.