What is the logic behind filling up a magic square? I have understood the algorithm of filling up a magic square of 3*3 or 5*5. I really do not know how to derive this
-
1$\begingroup$ @mixedmath: if I understand correctly, you're thinking of Latin squares, not magic squares. And those multiplication tables will only work if you delete the zero row/column, making them $2\times2$ and $4\times4$. $\endgroup$– Greg MartinCommented Jun 23, 2014 at 17:50
1 Answer
There are probably many ways of constructing magic squares. The one algorithm that I know of is based on a pair of orthogonal latin squares (aka Graeco-Latin square). IMO here is a slightly clearer description . Books on combinatorics describe their theory. They are (hopefully?) not the only route to magic squares, for example they don't exist for all $n$ (but I would guess that magic squares of all sizes exist).
I describe a construction that works when the size is a prime number $>2$. The same method can be extended to all sizes that are products of powers of primes $>2$, but that would necessitate familiarity with finite fields, and would take a bit longer.
So let $p$ be an odd prime. I begin by constructing two squares that are also orthogonal latin squares. To make the formulas a bit simpler I number them from $0$ to $p-1$. Let us pick two integers $a$ and $b$ such that $0<a<b<p$. In the first square I write in the cell at position $(i,j)$ the remainder of $ai+j$ modulo $p$, in the second I similarly use the formula $bi+j$. For example with $p=7$ and $a=1$, $b=2$ the two squares I get are $$ \begin{array}{|c|c|c|c|c|c|c|} \hline 0&1&2&3&4&5&6\\ \hline 1&2&3&4&5&6&0\\ \hline 2&3&4&5&6&0&1\\ \hline 3&4&5&6&0&1&2\\ \hline 4&5&6&0&1&2&3\\ \hline 5&6&0&1&2&3&4\\ \hline 6&0&1&2&3&4&5\\ \hline \end{array} $$ and $$ \begin{array}{|c|c|c|c|c|c|c|} \hline 0&1&2&3&4&5&6\\ \hline 2&3&4&5&6&0&1\\ \hline 4&5&6&0&1&2&3\\ \hline 6&0&1&2&3&4&5\\ \hline 1&2&3&4&5&6&0\\ \hline 3&4&5&6&0&1&2\\ \hline 5&6&0&1&2&3&4\\ \hline \end{array} $$ Them being latin squares means that each number occurs on each row and column exactly once.
Let's next write the two squares on top of each other. Then each cell will contain two numbers: the first from the table using $a$ and the second from the table using $b$. In our example case the result looks like
$$ \begin{array}{|c|c|c|c|c|c|c|} \hline 00&11&22&33&44&55&66\\ \hline 12&23&34&45&56&60&01\\ \hline 24&35&46&50&61&02&13\\ \hline 36&40&51&62&03&14&25\\ \hline 41&52&63&04&15&26&30\\ \hline 53&64&05&16&20&31&42\\ \hline 65&06&10&21&32&43&54\\ \hline \end{array} $$
The meaning of orthogonality of the two latin squares is that in this combination table all the pairs of $7\times7$ numbers appear exactly once. For example there are seven pairs with the first entry $0$ - one on each row and column, and there are no repetitions among the second entries of those pairs.
In the last step we turn this into a magic square. The idea is simply to replace the pair $(a,b)$ with the sum $pa+b$. Then because on each row and column all the $p$ possibilities for both $a$ and $b$ occur, the row and column sums will all be equal. Furthermore, because all the $(a,b)$ pairs occur exactly once, the sum $pa+b$ takes all the values from $0$ to $p^2-1$ exactly once. All this exactly as we wanted! In our example case we get the following magic square
$$ \begin{array}{|c|c|c|c|c|c|c|} \hline 0&8&16&24&32&40&48\\ \hline 9&17&25&33&41&42&1\\ \hline 18&26&34&35&43&2&10\\ \hline 27&28&36&44&3&11&19\\ \hline 29&37&45&4&12&20&21\\ \hline 38&46&5&13&14&22&30\\ \hline 47&6&7&15&23&31&39\\ \hline \end{array} $$ Here all the rows and columns have sum $168$.
If you want the numbers to be $1,2,\ldots, p^2$ instead of $0,1,2,\ldots, p^2-1$, then you can just increment all the entries of the resulting square by one.
If you want the diagonals to also have the same sums that can be achieved by not using either $1$ or $p-1$ as $a$ or $b$.
If you want more variety you can permute the numbers $0,1,\ldots,p-1$ in one or both of the latin squares any which way you want.
-
$\begingroup$ The necessary two MOLS of size $n$ exist at least when $n\not\equiv2\pmod4$. $\endgroup$ Commented Jun 23, 2014 at 21:58