Problem. Arrange in an grid so that every row, column and both main diagonals have the same sum, the magic constant (). Possible for every .
| Method | Idea | |
|---|---|---|
| odd | Siamese (de la Loubère) | walk up-right, wrap around; step down when the cell is taken |
| (doubly even) | complement the diagonals of every block | write in order, replace by on the marked cells |
| (singly even, ) | LUX (Conway) | take an odd square of order , blow each cell up into a block filled in an L, U or X pattern |
| impossible | forces |
| Order | Number of magic squares (rotations and reflections counted) |
|---|---|
| 3 | 8 (all are the Lo Shu square up to symmetry) |
| 4 | 7040 (880 up to symmetry; Frénicle, 1693) |
| 5 | (Schroeppel, 1973, computer count) |
vector<vector<int>> magicSquareAny(int n) { // any n != 2; values 1..n^2
vector<vector<int>> a(n, vector<int>(n));
if (n % 2 == 1) { // Siamese
int r = 0, c = n / 2;
for (int v = 1; v <= n * n; v++) {
a[r][c] = v;
int nr = (r - 1 + n) % n, nc = (c + 1) % n;
if (a[nr][nc]) nr = (r + 1) % n, nc = c;
r = nr, c = nc;
}
} else if (n % 4 == 0) { // doubly even
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
int v = i * n + j + 1;
a[i][j] = (i % 4 == j % 4 || i % 4 + j % 4 == 3) ? n * n + 1 - v : v;
}
} else { // LUX, n = 2k, k = 2m + 1 odd
int k = n / 2, m = (k - 1) / 2;
auto A = magicSquareAny(k);
static const int pat[3][2][2] = {{{4, 1}, {2, 3}}, {{1, 4}, {2, 3}}, {{1, 4}, {3, 2}}}; // L, U, X
for (int i = 0; i < k; i++) for (int j = 0; j < k; j++) {
int t = i <= m ? 0 : i == m + 1 ? 1 : 2; // rows 0..m: L, row m+1: U, the rest: X
if (j == m && i == m) t = 1; // swap the middle L with the U below it
if (j == m && i == m + 1) t = 0;
for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) a[2 * i + x][2 * j + y] = 4 * (A[i][j] - 1) + pat[t][x][y];
}
}
return a;
}Proof: the magic constant, the odd and doubly even methods, and the 3 × 3 squares
Constant. The rows partition the grid, so each row sums to .
Odd and doubly even. The Siamese walk and the diagonal-complement construction are proved magic in Grid and Matrix Constructions (same code).
. The middle row, middle column and both diagonals all pass through the centre and together cover every cell once, plus the centre three more times: , so , and every line through the centre pairs opposite cells summing to . If were in a corner, its row and column would each need two other numbers summing to from ( is opposite, on the diagonal); only works, and it cannot be used twice. So sits in the middle of an edge with opposite; the line through along the edge needs a pair summing to from the rest: , in two orders; then all other cells are forced by the opposite-sum rule. That gives (position of ) (order of ) squares, the symmetries of the Lo Shu. The 4 × 4 count 7040 was confirmed by an exhaustive search (rows as 4-subsets summing to 34, then row and column arrangements).
Proof: LUX is magic
Let be the odd magic square of order with constant . Block holds for the four entries of its pattern, so all values appear once. Pattern sums:
Pattern top row bottom row left column right column diagonal diagonal L 5 5 6 4 7 3 U 5 5 3 7 4 6 X 5 5 4 6 3 7 Each big row or column takes two entries from each of blocks of one block row or column, contributing from the offsets; each big diagonal takes two entries from each diagonal block of , contributing the same. It remains to show the pattern parts sum to every time; then every line sums to .
Rows: every pattern gives 5 per row. Columns: every block column has L’s, one U and X’s (the swap in column only exchanges an L and a U), so the left column gives and the right column . Main diagonal (blocks , using the entries): L for , U at (swapped) and at , X for : . Anti-diagonal (blocks , using the entries): L for , U at (swapped) and at (column of the U row), X after: . Since , the square is magic. Checked for every , .
Related notes
- Grid and Matrix Constructions
- Latin Squares and Euler’s 36 Officers (Euler built magic squares from orthogonal Latin squares)