Problem
Given an integer n
(where n % 4 == 0
), construct a normal magic square of order n
that contains the integers 1..n^2
exactly once, such that every row, every column, and the two main diagonals sum to the same magic constant M = n * (n^2 + 1) / 2
.
Examples
Example 1
|
|
Example 2
|
|
Notes
- This file focuses on doubly-even (4k) orders. Singly-even (4k+2) and odd-order (2k+1) constructions use different techniques and are listed under related problems.
- For
n
not divisible by4
, Method 1 below is not applicable; use other known constructions instead.
Solution
We show a standard and efficient method for doubly-even (n % 4 == 0
) magic squares based on complementing selected cells.
Method 1 — Complementation
Intuition
Fill the square left-to-right, top-to-bottom with 1..n^2
. Certain positions form a repeating 4x4 pattern; for those positions we replace the value x
with its complement n*n + 1 - x
. Complementing in this pattern produces the required symmetric distribution so that every row, column and diagonal sums to the magic constant.
Approach
- Create an
n x n
matrixarr
and fill it witharr[i][j] = n*i + j + 1
for0 <= i,j < n
. - For every cell
(i, j)
, computer = i % 4
andc = j % 4
. - If
(r == c) || (r + c == 3)
then replacearr[i][j]
withn*n + 1 - arr[i][j]
.- This condition marks the cells that must be complemented in every 4x4 block.
- Return
arr
as the magic square.
Edge cases / notes:
- The method requires
n % 4 == 0
. Ifn
is not divisible by 4, this method does not produce a valid magic square. - The algorithm is deterministic and produces the classical doubly-even magic square pattern.
Dry run (n = 4)
We illustrate Method 1 on n = 4
. Start with the filled matrix (left-to-right, top-to-bottom):
$$ \left[\begin{array}{c|c|c|c} \hline 1 & 2 & 3 & 4 \\ \hline 5 & 6 & 7 & 8 \\ \hline 9 & 10 & 11 & 12 \\ \hline 13 & 14 & 15 & 16 \\ \hline \end{array}\right] $$
The complement rule flips values in cells that match the 4x4 pattern (positions where r==c
or r+c==3
). We’ll show the changes step by step; complemented cells are shown in red.
- Top-left cell (position (0,0)) is complemented: replace
1
with16
(sincen*n+1 - 1 = 16
):
$$ \left[\begin{array}{c|c|c|c} \hline \color{red}{16} & 2 & 3 & 4 \\ \hline 5 & 6 & 7 & 8 \\ \hline 9 & 10 & 11 & 12 \\ \hline 13 & 14 & 15 & 16 \\ \hline \end{array}\right] $$
- Top-right cell (position (0,3)) is complemented: replace
4
with13
:
$$ \left[\begin{array}{c|c|c|c} \hline \color{red}{16} & 2 & 3 & \color{red}{13} \\ \hline 5 & 6 & 7 & 8 \\ \hline 9 & 10 & 11 & 12 \\ \hline 13 & 14 & 15 & 16 \\ \hline \end{array}\right] $$
- Bottom-left cell (position (3,0)) is complemented: replace
13
with4
:
$$ \left[\begin{array}{c|c|c|c} \hline \color{red}{16} & 2 & 3 & \color{red}{13} \\ \hline 5 & 6 & 7 & 8 \\ \hline 9 & 10 & 11 & 12 \\ \hline \color{red}{4} & 14 & 15 & 16 \\ \hline \end{array}\right] $$
- Bottom-right cell (position (3,3)) is complemented: replace
16
with1
:
$$ \left[\begin{array}{cccc} \hline \color{red}{16} & 2 & 3 & \color{red}{13} \\ \hline 5 & 6 & 7 & 8 \\ \hline 9 & 10 & 11 & 12 \\ \hline \color{red}{4} & 14 & 15 & \color{red}{1} \\ \hline \end{array}\right] $$
- Now complement the central 2x2 block positions that match the pattern (positions (1,1), (1,2), (2,1), (2,2)). Each value
x
is replaced by17 - x
:
$$ \left[\begin{array}{c|c|c|c} \hline \color{red}{16} & 2 & 3 & \color{red}{13} \\ \hline 5 & \color{red}{12} & \color{red}{11} & 8 \\ \hline 9 & \color{red}{10} & \color{red}{7} & 12 \\ \hline \color{red}{4} & 14 & 15 & \color{red}{1} \\ \hline \end{array}\right] $$
This final matrix is the standard 4x4 doubly-even magic square (each row, column and diagonal sums to 34).
Code
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
, because we fill then x n
grid once and then visit each cell once more to complement selected positions. - 🧺 Space complexity:
O(n^2)
, to store then x n
magic square.