Pandiagonal magic square

A pandiagonal magic square or panmagic square (also diabolic square, diabolical square or diabolical magic square) is a magic square with the additional property that the broken diagonals, i.e. the diagonals that wrap round at the edges of the square, also add up to the magic constant.

A pandiagonal magic square remains pandiagonally magic not only under rotation or reflection, but also if a row or column is moved from one side of the square to the opposite side. As such, an $$n \times n$$ pandiagonal magic square can be regarded as having $$8n^2$$ orientations.

3×3 pandiagonal magic squares
It can be shown that non-trivial pandiagonal magic squares of order 3 do not exist. Suppose the square
 * $$\begin{array}{|c|c|c|}

\hline \!\!\!\; a_{11} \!\!\! & \!\! a_{12}\!\!\!\!\; & \!\! a_{13} \!\!\\ \hline \!\!\!\; a_{21} \!\!\! & \!\! a_{22}\!\!\!\!\; & \!\! a_{23} \!\!\\ \hline \!\!\!\; a_{31} \!\!\! & \!\! a_{32}\!\!\!\!\; & \!\! a_{33} \!\!\\ \hline \end{array}$$ is pandiagonally magic with magic constant $s$. Adding sums $a_{11} + a_{22} + a_{33},$ $a_{12} + a_{22} + a_{32},$ and $a_{13} + a_{22} + a_{31}$ results in $3s$. Subtracting $a_{11} + a_{12} + a_{13}$ and $a_{31} + a_{32} + a_{33},$ we get $3a_{22} = s$. However, if we move the third column in front and perform the same argument, we obtain $3a_{22} = s$. In fact, using the symmetries of 3&thinsp;×&thinsp;3 magic squares, all cells must equal $\tfrac{1}{3}s$. Therefore, all 3&thinsp;×&thinsp;3 pandiagonal magic squares must be trivial.

However, if the magic square concept is generalized to include geometric shapes instead of numbers – the geometric magic squares discovered by Lee Sallows – a 3&thinsp;×&thinsp;3 pandiagonal magic square does exist.

4×4 pandiagonal magic squares


The smallest non-trivial pandiagonal magic squares are 4&thinsp;×&thinsp;4 squares. All 4&thinsp;×&thinsp;4 pandiagonal magic squares must be translationally symmetric to the form

Since each 2&thinsp;×&thinsp;2 subsquare sums to the magic constant, 4&thinsp;×&thinsp;4 pandiagonal magic squares are most-perfect magic squares. In addition, the two numbers at the opposite corners of any 3&thinsp;×&thinsp;3 square add up to half the magic constant. Consequently, all 4&thinsp;×&thinsp;4 pandiagonal magic squares that are associative must have duplicate cells.

All 4&thinsp;×&thinsp;4 pandiagonal magic squares using numbers 1-16 without duplicates are obtained by letting $a$ equal 1; letting $b$, $c$, $d$, and $e$ equal 1, 2, 4, and 8 in some order; and applying some translation. For example, with $b = 1$, $c = 2$, $d = 4$, and $e = 8$, we have the magic square

The number of 4&thinsp;×&thinsp;4 pandiagonal magic squares using numbers 1-16 without duplicates is 384 (16 times 24, where 16 accounts for the translation and 24 accounts for the 4! ways to assign 1, 2, 4, and 8 to $b$, $c$, $d$, and $e$).

5×5 pandiagonal magic squares
There are many 5&thinsp;×&thinsp;5 pandiagonal magic squares. Unlike 4&thinsp;×&thinsp;4 pandiagonal magic squares, these can be associative. The following is a 5&thinsp;×&thinsp;5 associative pandiagonal magic square:

In addition to the rows, columns, and diagonals, a 5&thinsp;×&thinsp;5 pandiagonal magic square also shows its magic constant in four "quincunx" patterns, which in the above example are:


 * 17+25+13+1+9 = 65 (center plus adjacent row and column squares)
 * 21+7+13+19+5 = 65 (center plus the remaining row and column squares)
 * 4+10+13+16+22 = 65 (center plus diagonally adjacent squares)
 * 20+2+13+24+6 = 65 (center plus the remaining squares on its diagonals)

Each of these quincunxes can be translated to other positions in the square by cyclic permutation of the rows and columns (wrapping around), which in a pandiagonal magic square does not affect the equality of the magic constants. This leads to 100 quincunx sums, including broken quincunxes analogous to broken diagonals.

The quincunx sums can be proved by taking linear combinations of the row, column, and diagonal sums. Consider the pandiagonal magic square
 * $$\begin{array}{|c|c|c|c|c|}

\hline \!\!\!\; a_{11} \!\!\! & \!\! a_{12} \!\!\! & \!\! a_{13} \!\!\! & \!\! a_{14} \!\!\! & \!\! a_{15} \!\!\\ \hline \!\!\!\; a_{21} \!\!\! & \!\! a_{22} \!\!\! & \!\! a_{23} \!\!\! & \!\! a_{24} \!\!\! & \!\! a_{25} \!\!\\ \hline \!\!\!\; a_{31} \!\!\! & \!\! a_{32} \!\!\! & \!\! a_{33} \!\!\! & \!\! a_{34} \!\!\! & \!\! a_{35} \!\!\\ \hline \!\!\!\; a_{41} \!\!\! & \!\! a_{42} \!\!\! & \!\! a_{43} \!\!\! & \!\! a_{44} \!\!\! & \!\! a_{45} \!\!\\ \hline \!\!\!\; a_{51} \!\!\! & \!\! a_{52} \!\!\! & \!\! a_{53} \!\!\! & \!\! a_{54} \!\!\! & \!\! a_{55} \!\!\\ \hline \end{array}$$ with magic constant $s$. To prove the quincunx sum $$a_{11} + a_{15} + a_{33} + a_{51} + a_{55} = s$$ (corresponding to the 20+2+13+24+6 = 65 example given above), we can add together the following:
 * 3 times each of the diagonal sums $$a_{11} + a_{22} + a_{33} + a_{44} + a_{55}$$ and $$a_{15} + a_{24} + a_{33} + a_{42} + a_{51}$$,
 * The diagonal sums $$a_{11} + a_{25} + a_{34} + a_{43} + a_{52}$$, $$a_{12} + a_{23} + a_{34} + a_{45} + a_{51}$$, $$a_{14} + a_{23} + a_{32} + a_{41} + a_{55}$$, and $$a_{15} + a_{21} + a_{32} + a_{43} + a_{54}$$,
 * The row sums $$a_{11} + a_{12} + a_{13} + a_{14} + a_{15}$$ and $$a_{51} + a_{52} + a_{53} + a_{54} + a_{55}$$.

From this sum, subtract the following:
 * The row sums $$a_{21} + a_{22} + a_{23} + a_{24} + a_{25}$$ and $$a_{41} + a_{42} + a_{43} + a_{44} + a_{45}$$,
 * The column sum $$a_{13} + a_{23} + a_{33} + a_{43} + a_{53}$$,
 * Twice each of the column sums $$a_{12} + a_{22} + a_{32} + a_{42} + a_{52}$$ and $$a_{14} + a_{24} + a_{34} + a_{44} + a_{54}$$.

The net result is $$5a_{11} + 5a_{15} + 5a_{33} + 5a_{51} + 5a_{55} = 5s$$, which divided by 5 gives the quincunx sum. Similar linear combinations can be constructed for the other quincunx patterns $$a_{23} + a_{32} + a_{33} + a_{34} + a_{43}$$, $$a_{13} + a_{31} + a_{33} + a_{35} + a_{53}$$, and $$a_{22} + a_{24} + a_{33} + a_{42} + a_{44}$$.

(4n+2)×(4n+2) pandiagonal magic squares with nonconsecutive elements
No pandiagonal magic square exists of order $$4n+2$$ if consecutive integers are used. But certain sequences of nonconsecutive integers do admit order-($$4n+2$$) pandiagonal magic squares.

Consider the sum 1+2+3+5+6+7 = 24. This sum can be divided in half by taking the appropriate groups of three addends, or in thirds using groups of two addends:


 * 1+5+6 = 2+3+7 = 12


 * 1+7 = 2+6 = 3+5 = 8

An additional equal partitioning of the sum of squares guarantees the semi-bimagic property noted below:


 * 12 + 52 + 62 = 22 + 32 + 72 = 62

Note that the consecutive integer sum 1+2+3+4+5+6 = 21, an odd sum, lacks the half-partitioning.

With both equal partitions available, the numbers 1, 2, 3, 5, 6, 7 can be arranged into 6&thinsp;×&thinsp;6 pandigonal patterns $A$ and $B$, respectively given by:

Then $$7A + B - 7C$$ (where $C$ is the magic square with 1 for all cells) gives the nonconsecutive pandiagonal 6&thinsp;×&thinsp;6 square:

with a maximum element of 49 and a pandiagonal magic constant of 150. This square is pandiagonal and semi-bimagic, that means that rows, columns, main diagonals and broken diagonals have a sum of 150 and, if we square all the numbers in the square, only the rows and the columns are magic and have a sum of 5150.

For 10th order a similar construction is possible using the equal partitionings of the sum 1+2+3+4+5+9+10+11+12+13 = 70:


 * 1+3+9+10+12 = 2+4+5+11+13 = 35


 * 1+13 = 2+12 = 3+11 = 4+10 = 5+9 = 14


 * 12 + 32 + 92 + 102 + 122 = 22 + 42 + 52 + 112 + 132 = 335 (equal partitioning of squares; semi-bimagic property)

This leads to squares having a maximum element of 169 and a pandiagonal magic constant of 850, which are also semi-bimagic with each row or column sum of squares equal to 102,850.

(6n±1)×(6n±1) pandiagonal magic squares
A $$(6n \pm 1) \times (6n \pm 1)$$ pandiagonal magic square can be built by the following algorithm. 1. Set up the first column of the square with the first $6n \pm 1$ natural numbers.

2. Copy the first column into the second column but shift it ring-wise by 2 rows.

3. Continue copying the current column into the next column with ring-wise shift by 2 rows until the square is filled completely.

4. Build a second square and copy the transpose of the first square into it.

5. Build the final square by multiplying the second square by $6n \pm 1$, adding the first square and subtract $6n \pm 1$ in each cell of the square. Example: $A + (6n \pm 1)A^T - (6n \pm 1)B$, where $A$ is the magic square with all cells as 1.

4n×4n pandiagonal magic squares
A $$4n \times 4n$$ pandiagonal magic square can be built by the following algorithm. 1. Put the first $2n$ natural numbers into the first row and the first $2n$ columns of the square.

2. Put the next $2n$ natural numbers beneath the first $2n$ natural numbers in reverse. Each vertical pair must have the same sum.

3. Copy that $2 \times 2n$ rectangle $2n-1$ times beneath the first rectangle.

4. Copy the left $4n \times 2n$ rectangle into the right $4n \times 2n$ rectangle but shift it ring-wise by one row.

5. Build a second $4n \times 4n$ square and copy the first square into it but turn it by 90°.

6. Build the final square by multiplying the second square by $4n$, adding the first square and subtract $4n$ in each cell of the square. Example: $A + 4nB - 4nC$, where $B$ is the magic square with all cells as 1. If we build a $$4n \times 4n$$ pandiagonal magic square with this algorithm then every $$2 \times 2$$ square in the $$4n \times 4n$$ square will have the same sum. Therefore, many symmetric patterns of $$4n$$ cells have the same sum as any row and any column of the $$4n \times 4n$$ square. Especially each $$2n \times 2$$ and each $$2 \times 2n$$ rectangle will have the same sum as any row and any column of the $$4n \times 4n$$ square. The $$4n \times 4n$$ square is also a most-perfect magic square.

(6n+3)×(6n+3) pandiagonal magic squares
A $$(6n+3) \times (6n+3)$$ pandiagonal magic square can be built by the following algorithm. 1. Create a $(2n+1) \times 3$ rectangle with the first $6n+3$ natural numbers so that each column has the same sum. You can do this by starting with a 3&thinsp;×&thinsp;3 magic square and set up the rest cells of the rectangle in meander-style. You can also use the pattern shown in the following examples.

2. Put this rectangle in the left upper corner of the $(6n+3) \times (6n+3)$ square and two copies of the rectangle beneath it so that the first 3 columns of the square are filled completely.

3. Copy the left 3 columns into the next 3 columns, but shift it ring-wise by 1 row.

4. Continue copying the current 3 columns into the next 3 columns, shifted ring-wise by 1 row, until the square is filled completely.

5. Build a second square and copy the transpose of the first square into it.

6. Build the final square by multiplying the second square by $6n+3$, adding the first square and subtract $6n+3$ in each cell of the square.

Example: $A + (6n+3)A^T - (6n+3)B$, where $A$ is the magic square with all cells as 1.