# Can there be a magic square with equal diagonal sums different from equal row and column sums?

I got a task in programming a program that can detect whether a 4x4 square is a magic square or not. At first, I wrote code that met the requirements for all given examples but I noticed one flaw. I used 2 variables to indicate sums. I used them once to calculate the sums of the rows and columns and compare them, then I reset them back to 0 and used them to calculate the diagonal sums and check if they were equal. The thing was, is that I did not actually compare the diagonal sums to the original row and column sums, and that got me thinking. Can there exist a "magic square" where the diagonal sums are equal and the row and column sums are equal, but the diagonal sums are different from the row and column sums? Is there any actual way to prove this? I tried to come up with examples but nothing came to me. An example would disprove this and make me rewrite my code. For simplicity, I would rather know about a 4x4 square, but if you can I'll be happy to hear a proof for any $n$ x $n$ square. Thanks in advance.

Edit: I already check to see if the integers are all different, so I'd rather know if one exists where all of the integers are different.

## 4 Answers

I wrote a program to search for $4\times 4$ examples by brute force. Here is one. $$\begin{matrix} 1&11&10&12\cr 3&15&7&9\cr 14&2&13&5\cr 16&6&4&8\cr \end{matrix}$$

• Yeah!! :) Nice work! – Bram28 Jan 24 '18 at 22:24
• Well done! Reworked my code. Thanks. – Nick S. Jan 25 '18 at 21:22

I assume you are looking for one with not just 'all different numbers', for that is trivial: just take any $n \times n$ magic square and add the same amount (larger than $n^2$) to the cells on the diagonals. For example, you can go from:

\begin{array}{|c|c|c|c|} \hline 16&3&2&13\\ \hline 5&10&11&8\\ \hline 9&6&7&12\\ \hline 4&15&14&1\\ \hline \end{array}

to:

\begin{array}{|c|c|c|c|} \hline 116&3&2&113\\ \hline 5&110&111&8\\ \hline 9&106&107&12\\ \hline 104&15&14&101\\ \hline \end{array}

... and that's just too easy! So, I assume you mean that you have to use all numbers $1$ through $n^2$.

Well, after trying a bunch of things I am fairly convinced that you cannot have a $4 \times 4$ square with numbers $1$ through $16$ where are rows and columns sum p to the same amount (this is actually called a 'semi-magic square') but where the diagonals sum up to the same amount, yet different from the rows and columns. In all $4 \times 4$ semi-magic squares that were not $4 \times 4$ magic squares that I looked at, I found the diagonals still adding up to exactly twice the sum of a row. I don't have a proof though that this is really impossible.

I did, however, find a $6 \times6$ square using numbers $1$ through $36$ with all rows and columns adding up to $111$ but both diagonals adding up to only $97$:

\begin{array}{|c|c|c|c|c|c|} \hline 6&34&1&28&24&18\\ \hline 14&7&36&4&20&30\\ \hline 33&3&31&2&17&25\\ \hline 27&22&23&16&12&11\\ \hline 10&13&5&35&29&19\\ \hline 21&32&15&26&9&8\\ \hline \end{array}

And so, the answer to your question is Yes! (But no, I did not look at the $5 \times 5$ case (although my guess is that you can find an example of what you want for the $5 \times 5$ case and up), and no, I did not create this example in a systematic way: I started with a known semi-magic $6 \times 6$ square and kept swapping rows and columns in a semi-random fashion until I found this one).

Finally, in my research I found that there are many different kinds of magic squares (I had no idea!), such as these 'semi-magic squares' or 'extremely magic squares', or .... but what you are asking about I did not see a name for. Given that they apparently exist you should definitely come up with a name for these!

EDIT

Aha! As I thought, it also works for $n=5$. Here is one:

\begin{array}{|c|c|c|c|c|} \hline 19&6&15&2&23\\ \hline 9&17&10&13&16\\ \hline 21&24&14&5&1\\ \hline 4&11&8&20&22\\ \hline 12&7&18&25&3\\ \hline \end{array}

Rows and columns sum to $65$, but columns sum to $73$

EDIT 2: Aha! I was wrong about the $4 \times 4$: it is possible to have one!! See Taneli Huuskonen's answer.

• If you swap the top two rows in your first example, you get a $4\times 4$ semimagic square whose diagonals sum up to 36 rather than 68. Their sums are different, though (20 and 16). – Taneli Huuskonen Jan 24 '18 at 16:28
• @TaneliHuuskonen Oh, right, yes! OK, so maybe it is possible for a 4x4? Hmmmm .... Thanks! – Bram28 Jan 24 '18 at 19:18
• Yes, I just posted an example. – Taneli Huuskonen Jan 24 '18 at 20:44

Yes, here's one:

\begin{matrix} 1 &2&2&1\\2&1&1&2\\2&1&1&2\\1&2&2&1 \end{matrix}

• Thanks! This still is a reason to fix my code, but as I know a magic square is defined to have each cell with a different integer, so I'm not sure if to count that as a magic square, but what about if it has to be all different integers? – Nick S. Jan 21 '18 at 16:19

there is a geometrical method to systematically construct not even (2n+1) magic squares, quite easy, even by hand with a pen! I will post an example for 5x5 and 7x7 squares, as soon as I get hold of a scanner. see below a 5x5 one Greg

3 16 9 22 15 20 8 21 14 2 7 25 13 1 19 24 12 5 18 6 11 4 17 10 23