Magic Rectangle Calculator

Enter the number of rows p and columns q — any size. The calculator returns a magic rectangle whenever one exists, and a nearly magic rectangle otherwise, covering every order for which either exists. Each result is verified from first principles before it is displayed.

Constructions: De Los Reyes–Das–Midha–Vellaisamy (2009) · Chai–Das–Midha (2013) · Chai–Singh–Stufken (2019)

Try:

The mathematics — one divisibility fact decides everything

A magic rectangle wants every row to carry the same total and every column to carry the same total. Whether that is even possible comes down to a single question about whole numbers.

Average entry
A = pq + 12
Every row must sum to
q · pq + 12
Every column must sum to
p · pq + 12

The integers 1, …, pq average to A = (pq+1)/2, so a row of q entries must total qA and a column of p entries must total pA. Those totals are whole numbers only when p and q have the same parity — both odd makes pq+1 even, both even makes q/2 and p/2 whole. Mix an odd with an even and both magic constants become fractions: no magic rectangle can exist, and the nearly magic rectangle — sums differing by exactly 1 — is provably the best achievable.

The parity map — click any case to build one

q odd
q even
p odd
Magic ✓
pq + 1 is even, so both constants are whole.
try 7 × 11 →
Nearly magic ◐
Column sums equal; row sums differ by exactly 1.
try 7 × 12 →
p even
Nearly magic ◐
Column sums equal; row sums differ by exactly 1.
try 12 × 7 →
Magic ✓
p/2 and q/2 are whole, so both constants are whole.
try 8 × 10 →
2 × 2 — the lone exception. Equal row and column sums would force two of the four entries to be equal. See for yourself →
1 × n (n > 2) — impossible. A single line of distinct integers cannot have equal column sums. See for yourself →

Two ten-second proofs by example

1221045639
111398739
131313131313
A 2 × 6 magic rectangle. Both dimensions even: every row makes 39, every column makes 13 — perfectly magic.
15410
62311
777
A 2 × 3 nearly magic rectangle. Even × odd: the columns all make 7, and the rows land on 10 and 11 — off by exactly one, the theoretical best.

Four papers, one complete answer

  1. 1999Magic rectangles revisited T. R. Hagedorn · Discrete Mathematics 207, 65–72
    Settles existence once and for all (building on constructions studied since T. Harmuth in the 1880s): a p × q magic rectangle exists exactly when p and q share a parity, both exceed 1, and (p, q) ≠ (2, 2).
  2. 2009On a method to construct magic rectangles of even order J. P. De Los Reyes, A. Das, C. K. Midha, P. Vellaisamy · Utilitas Mathematica 80, 277–284
    The even × even recipe used here: write 1…pq in a serpentine grid, then reverse the middle rows (Case A) or perform four precise reversal-and-swap steps (Case B, both half-dimensions odd).
  3. 2013Construction of magic rectangles of odd order F.-S. Chai, A. Das, C. Midha · Australasian J. Combinatorics 55, 131–144
    The odd × odd engine: a pattern matrix Gp with constant column sums is lifted to Hp, then a small set of within-column interchanges (Cases I–III of Theorem 4.1) equalises the row sums; a composition theorem handles q divisible by 3. This calculator reproduces the paper's 7 × 11 and 13 × 19 examples digit for digit.
  4. 2019Nearly magic rectangles F.-S. Chai, R. Singh, J. Stufken · J. Combinatorial Designs 27(6), 368–376
    Defines the nearly magic rectangle for mixed parity and constructs one for every order where it exists: 2 × q building blocks stacked and permuted by a Kronecker-pattern matrix (p ≡ 2 mod 4), or column-block exchanges (p ≡ 0 mod 4).