# Constructing a magic square

I am currently learning about magic squares and I want to construct a magic square. How do I construct a 6-by-6 or a 7-by-7 filled magic square, using the integers 0 to 35 or 0 to 48.

• I don't think there are magic squares with $0$ in it. – servabat Feb 20 '15 at 13:44
• @servabat Yes, there are. Put a $0$ on an $1\times 1$ square. Or take any magic square and choose any number $n$ in it. Substract $n$ to every number in square, and you will obtain another magic square that has a $0$. – ajotatxe Feb 20 '15 at 13:50
• @ajotatxe : Well, if you put $0$ in $1 \times 1$ square, the sum is equal to $0$ and not $1 = \frac{1(1^2 + 1)}{2}$ (the magic sum). Maybe haven't we the same definition of magic squares then. – servabat Feb 20 '15 at 13:52
• @servabat I think taht we are working with different definitions of magic squares. My conditions are that every row, column and diagonal add up the same number. You add the condition of that the numbers must be from $1$ to $n^2$. – ajotatxe Feb 20 '15 at 13:54
• @ajotatxe : Well, indeed, I was actually quite stupid, if you substract $1$ in any row of a magic square with numbers from $1$ to $n^2$ you get a magic square with numbers from $0$ to $n^2 - 1$ of magic sum $\frac{n(n^2+1)}{2} - 3$ I guess. – servabat Feb 20 '15 at 13:58

Here is a method for producing $n\times n$ magic squares where $n$ is odd.

Choose $p$ and $q$ however you like. Start with an empty $n\times n$ grid. Put the number $q$ in the top center square.

Go up one row and move one square to the right. This takes you outside the $n\times n$ grid, above the top row, so return to the bottom square in the same column and write the number $p+q$. Go up one row and move one square to the right and write $2p+q$. Continue in this fashion; if you move up past the top row, return the the bottom square in the same column; if you move right past the last column, return to the leftmost square in the same row. If the next square is already filled, or if you reach the upper-right square in the array, move down one square and continue. At each step write a number that is $p$ more than the previous number.

For $n=5, p=1, q=1$ this produces

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

If you don't care about the sum being correct on the diagonals, you can start anywhere, not just in the top center.

• Are there different answers, because I did this and got a different answer? – Daniella Feb 20 '15 at 17:56
• The method I described says exactly what numbers to put into each square, so if you followed the directions correctly, with the same $p$ and $q$, you should get the same result. – MJD Feb 20 '15 at 18:16
• Okay, thanks i get it now. – Daniella Feb 20 '15 at 18:17
• But I have to do it using the numbers 0-24 – Daniella Feb 20 '15 at 18:20
• Then take $q=0, p=1$. – MJD Feb 20 '15 at 18:25

I have thought about this problem some time ago. I don't know any theorems or proofs, but my method for an $n\times n$ square was the following:

• Make chips marked with the numbers from $0$ to $n-1$, $2n$ chips for each number. Of these $2n$ chips, make $n$ different from the other $n$ chips (for example, $n$ white chips and $n$ red chips).
• On an empty $n\times n$ board, you must put two chips in each square: a red one and a white one. If a square has a red $j$ and a white $k$, this represents that the number $nj+k$ is in this square (that is: $j$ and $k$ are the digits of the number in base $n$).
• In each row and column there must be the $n$ different numbers for each color, in a similar way to a Sudoku.
• You must also watch for the diagonals.

This way, I was able to make a 5x5 square and a 6x6 square. By the way, I think (but I don't have any proof either) that if $n$ is odd, the central square must contain the arithmetic mean of the row.

I know that this is far from a deterministic algorithm, but it is an idea to easen the work. Surely, there will be somebody that has a better answer.