Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Magic squares: core definitions and counting functions

Definition
MagicSquares

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

combinatoricsenumerative-combinatoricsmagic-squares

Core vocabulary for the theory of magic squares: an n×nn \times nn×n array (Square), its row, column, main-diagonal, anti-diagonal and broken-diagonal sums, and the standard predicates used in the literature.

A semi-magic square of line sum sss has every row and every column summing to sss; it is magic if both main diagonals also sum to sss, and panmagic (pandiagonal) if every broken diagonal in both directions does. A square is associative with constant ccc when centrally opposite cells sum to ccc, and compact with constant ccc when every 2×22 \times 22×2 block -- including the ones that wrap around the edges -- sums to ccc.

A square is normal of order nnn when its entries are exactly the integers 1,…,n21, \dots, n^{2}1,…,n2, each occurring once. Then magicConstant n =n(n2+1)/2= n(n^{2}+1)/2=n(n2+1)/2 is the common line sum, and complement is the involution M↦n2+1−MM \mapsto n^{2}+1-MM↦n2+1−M.

Finally the module fixes the four classical counting functions: Hn(t)H_{n}(t)Hn​(t) (semi-magic), Mn(t)M_{n}(t)Mn​(t) (magic), Pn(t)P_{n}(t)Pn​(t) (panmagic) and Sn(t)S_{n}(t)Sn​(t) (symmetric magic), each counting squares of order nnn with nonnegative integer entries and line sum ttt. Since every entry of such a square is at most ttt, searching over arrays with entries in {0,…,t}\{0,\dots,t\}{0,…,t} loses nothing.

Formalization Note Entries are indexed by Fin n, so broken diagonals are obtained by addition modulo nnn and the anti-diagonal by Fin.rev. The counting functions are cardinalities of finsets of arrays over Fin (t+1).

Definition code
import Mathlib.Data.Matrix.Basic
import Mathlib.Data.Fin.Basic
import Mathlib.Algebra.Group.Fin.Basic
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Data.Finset.Card
import Mathlib.Order.Interval.Finset.Nat

set_option autoImplicit false

/-!
# Magic squares: core vocabulary

This module fixes the combinatorial vocabulary used throughout the magic-square
formalization. It contains only definitions (plus their immediate `simp`
companions); every substantive statement is submitted separately as a theorem.

Conventions follow Beck, Cohen, Cuomo and Gribelyuk, *The number of "magic"
squares, cubes and hypercubes*, Amer. Math. Monthly **110** (2003), 707--717
(arXiv:math/0201013) for the weak (nonnegative-integer) reading, and the
classical literature for the *normal* reading where the entries are exactly
`1, …, n²`.

An `n × n` array is `Square n α`, i.e. `Matrix (Fin n) (Fin n) α`. All sums are
taken over `Finset.univ`, and indices on broken diagonals are read modulo `n`.
-/

namespace MagicSquares

/-- An `n × n` array with entries in `α`. -/
abbrev Square (n : ℕ) (α : Type*) := Matrix (Fin n) (Fin n) α

variable {n : ℕ} {α : Type*}

/-! ## Line sums -/

/-- The sum of the entries in row `i`. -/
def rowSum [AddCommMonoid α] (M : Square n α) (i : Fin n) : α :=
  ∑ j, M i j

/-- The sum of the entries in column `j`. -/
def colSum [AddCommMonoid α] (M : Square n α) (j : Fin n) : α :=
  ∑ i, M i j

/-- The sum of the entries on the main (descending) diagonal. -/
def diagSum [AddCommMonoid α] (M : Square n α) : α :=
  ∑ i, M i i

/-- The sum of the entries on the anti-diagonal, i.e. the cells `(i, n-1-i)`. -/
def antiDiagSum [AddCommMonoid α] (M : Square n α) : α :=
  ∑ i, M i (Fin.rev i)

/-- The sum of the entries on the broken descending diagonal of offset `k`,
i.e. the cells `(i, i + k)` with the column index read modulo `n`.
For `k = 0` this is `diagSum`. -/
def brokenDiagSum [AddCommMonoid α] (M : Square n α) (k : Fin n) : α :=
  ∑ i, M i (i + k)

/-- The sum of the entries on the broken ascending diagonal of offset `k`,
i.e. the cells `(i, n - 1 - i + k)`.
For `k = 0` this is `antiDiagSum`. -/
def brokenAntiDiagSum [AddCommMonoid α] (M : Square n α) (k : Fin n) : α :=
  ∑ i, M i (Fin.rev i + k)

/-- The sum of all entries of the array. -/
def totalSum [AddCommMonoid α] (M : Square n α) : α :=
  ∑ i, ∑ j, M i j

/-! ## The magic predicates -/

/-- A **semi-magic square** of line sum `s`: every row and every column sums to
`s`. Entries are not required to be distinct. -/
def IsSemiMagic [AddCommMonoid α] (M : Square n α) (s : α) : Prop :=
  (∀ i, rowSum M i = s) ∧ (∀ j, colSum M j = s)

/-- A **magic square** of line sum `s`: semi-magic, and both main diagonals sum
to `s` as well. Entries are not required to be distinct. -/
def IsMagic [AddCommMonoid α] (M : Square n α) (s : α) : Prop :=
  IsSemiMagic M s ∧ diagSum M = s ∧ antiDiagSum M = s

/-- A **panmagic (pandiagonal) square** of line sum `s`: semi-magic, and *every*
broken diagonal in both directions sums to `s`. -/
def IsPanMagic [AddCommMonoid α] (M : Square n α) (s : α) : Prop :=
  IsSemiMagic M s ∧
    (∀ k, brokenDiagSum M k = s) ∧
      (∀ k, brokenAntiDiagSum M k = s)

/-- An **associative (regular) square** with complement constant `c`: every pair
of cells centrally opposite each other sums to `c`. -/
def IsAssociative [Add α] (M : Square n α) (c : α) : Prop :=
  ∀ i j, M i j + M (Fin.rev i) (Fin.rev j) = c

/-- A **compact square** with block constant `c`: every `2 × 2` block, including
those that wrap around the edges, sums to `c`. -/
def IsCompact [NeZero n] [AddCommMonoid α] (M : Square n α) (c : α) : Prop :=
  letI := Fin.instAddMonoidWithOne n
  ∀ i j : Fin n, M i j + M i (j + 1) + M (i + 1) j + M (i + 1) (j + 1) = c

/-- A **symmetric** square: `M` equals its transpose. -/
def IsSymmetric (M : Square n α) : Prop :=
  ∀ i j, M i j = M j i

/-- A **bimagic square**: `M` is magic with line sum `s`, and the array of
squared entries is magic with line sum `s₂`. -/
def IsBimagic (M : Square n ℕ) (s s₂ : ℕ) : Prop :=
  IsMagic M s ∧ IsMagic (fun i j => (M i j) ^ 2) s₂

/-! ## Normal squares -/

/-- A **normal** square of order `n`: the entries are exactly the integers
`1, 2, …, n²`, each occurring once.

Formally this is stated as (i) every entry lies in `Icc 1 (n^2)` and (ii) the
index-to-entry map is injective. Together these say the entries are a
permutation of `1, …, n²`. -/
def IsNormal (M : Square n ℕ) : Prop :=
  (∀ p : Fin n × Fin n, 1 ≤ M p.1 p.2 ∧ M p.1 p.2 ≤ n ^ 2) ∧
    Function.Injective (fun p : Fin n × Fin n => M p.1 p.2)

/-- The magic constant of a normal magic square of order `n`, namely
`n (n² + 1) / 2`. -/
def magicConstant (n : ℕ) : ℕ := n * (n ^ 2 + 1) / 2

/-- The complementary square of a normal square: `M ↦ n² + 1 - M`. -/
def complement (M : Square n ℕ) : Square n ℕ :=
  fun i j => n ^ 2 + 1 - M i j

/-! ## Counting functions

Every entry of a (semi-)magic square with nonnegative entries and line sum `t`
is at most `t`, so the finite search space `Square n (Fin (t+1))` is lossless. -/

noncomputable section

/-- All semi-magic squares of order `n` and line sum `t`. -/
def semiMagicSquares (n t : ℕ) : Finset (Square n (Fin (t + 1))) :=
  by classical exact Finset.univ.filter fun M => IsSemiMagic (fun i j => (M i j : ℕ)) t

/-- All magic squares of order `n` and line sum `t`. -/
def magicSquares (n t : ℕ) : Finset (Square n (Fin (t + 1))) :=
  by classical exact Finset.univ.filter fun M => IsMagic (fun i j => (M i j : ℕ)) t

/-- All panmagic squares of order `n` and line sum `t`. -/
def panMagicSquares (n t : ℕ) : Finset (Square n (Fin (t + 1))) :=
  by classical exact Finset.univ.filter fun M => IsPanMagic (fun i j => (M i j : ℕ)) t

/-- All symmetric magic squares of order `n` and line sum `t`. -/
def symmetricMagicSquares (n t : ℕ) : Finset (Square n (Fin (t + 1))) :=
  by classical exact (Finset.univ.filter (fun M : Square n (Fin (t + 1)) => IsMagic (fun i j => (M i j : ℕ)) t ∧ IsSymmetric (fun i j => (M i j : ℕ))))

/-- `H_n(t)`: the number of semi-magic squares of order `n` and line sum `t`. -/
def semiMagicCount (n t : ℕ) : ℕ := (semiMagicSquares n t).card

/-- `M_n(t)`: the number of magic squares of order `n` and line sum `t`. -/
def magicCount (n t : ℕ) : ℕ := (magicSquares n t).card

/-- `P_n(t)`: the number of panmagic squares of order `n` and line sum `t`. -/
def panMagicCount (n t : ℕ) : ℕ := (panMagicSquares n t).card

/-- `S_n(t)`: the number of symmetric magic squares of order `n` and line sum `t`. -/
def symmetricMagicCount (n t : ℕ) : ℕ := (symmetricMagicSquares n t).card

end

end MagicSquares
Source
Beck, Cohen, Cuomo & Gribelyuk, The number of ``magic'' squares, cubes and hypercubes, Amer. Math. Monthly 110 (2003), 707-717; arXiv:math/0201013v3, Section 1 (definitions) and Section 2 (Theorem 1).
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