7
$\begingroup$

In a 3x3 grid, I'd have to put numbers from 1 to 9 in a manner so that respective row, column and diagonal add up to 15.

I have only been able to come up with one solution:

$$\begin{array}{ccc} 6 & 1 & 8 \\ 7 & 5 & 3 \\ 2 & 9 & 4 \end{array}$$

Through some calculations and trial and error method.

Is there any strategy or way of approach to this problem, or is trial and error method the solution to it?

$\endgroup$
  • 1
    $\begingroup$ These are called "magic squares". $\endgroup$ – Hovercouch Jul 25 '14 at 22:25
  • $\begingroup$ For the case of a three-by-three magic square, there is only one solution. $\endgroup$ – Joe Z. Jul 30 '14 at 1:40
  • $\begingroup$ This question might just have the lowest "views:upvotes" ratio I have ever seen (2:56585) $\endgroup$ – ghosts_in_the_code Sep 22 '15 at 18:27
  • $\begingroup$ @ghosts_in_the_code For a non-zero vote count, certainly. How the heck did this get so many views? Did it make Hot Network Questions for a year?! $\endgroup$ – Roland Sep 22 '15 at 18:29
15
$\begingroup$

There is a general, very simple, algorithm for generating any magic square which has an odd number of rows/columns as follows:

  1. Start in the middle of the top row and enter 1.
  2. Move Up 1 and Right 1 wrapping both vertically and horizontally when you leave the grid *(see note below).
  3. If that square is empty enter the next number; if the square is not empty put the next number underneath the last number you entered.
  4. Repeat 2&3 until the grid is complete. All rows, columns and the two diagonals will sum to the same value.

Here is the 5x5 square:

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

As the square square is symmetric there are eight symmetries. You can also get these symmetries by a simple variation of the start square and direction used in step 2.

*Note: So as you are in the middle of the top row on the first move you want to place the next number in the next column of the row above. The row above does not exist so move to the last row of the square in the same column. If you were in the last column you would move to the first column. If you look at the example of the 5x5 at number 15. The next position is the square up and to the right of 15 which wraps on both the row and column to point to the lower right square which has 11 in it. As that square is not empty we placed 16 underneath 15.

$\endgroup$
  • $\begingroup$ Thank you @ Alan for explaining it so well and generalising it. Does this method have a name? It helped me very much. :) $\endgroup$ – Freya Jul 31 '14 at 18:05
  • $\begingroup$ @Freya No problem, unfortunately I do not know if this method has a name, my father taught me this over 35 years ago and it all came back when I saw your question :). There should be an algorithm for magic square with an even number of rows/columns somewhere. $\endgroup$ – Alan Aug 1 '14 at 12:36
  • $\begingroup$ @ Alan I see. It is a very nice way and I am glad to have been taught this. :) $\endgroup$ – Freya Aug 8 '14 at 15:42
  • $\begingroup$ This link me be of use for even squares; m.wikihow.com/Solve-a-Magic-Square (@Freya and answerer) $\endgroup$ – warspyking Dec 5 '14 at 0:01
  • $\begingroup$ We call this as rolling numbers. as we have to roll the paper edges when we want to move up and left $\endgroup$ – Kishore Relangi Jul 26 '16 at 7:02
7
$\begingroup$

There are 8 equations which must must add to $15$

$$a+b+c=15$$ $$d+e+f=15$$ $$ g+h+I=15$$ $$ a+d+g =15$$ $$b+e+h =15$$ $$c+f+I=15$$ $$ a+e+I =15$$ $$ c+e+g=15$$ I would place these in excel personally. So adding equations $2,5,7,8$ and then subtracting equations $1,2,3$ yields $3e=15$ so $ e=5$.

No others are solvable without guessing one but from there you just solve them one at a time. Guess $1$ is in a corner, requires $9$ in the other corner. There must be two pairs which add to $14$. The only one is $8+6$ so $1$ cannot be in the corner.

Guess $1$ not in a corner, $9$ must still be opposite to it. $8$ and $6$ must be in the adjacent corners. The other corners can be solved by the diagonal equations ($2$ and $4$). Finally the last two squares can be easily solved.

There are only 8 different solutions and because this logic eliminates any others; they are all mirrors/rotations of the one you already posted.

Of course there are some made from larger squares....

$\endgroup$
4
$\begingroup$

It is simple. Put

$$\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}$$

…then make cross lines as shown in the picture below. Then it becomes 3x3 boxes. For the remaining boxes, put the number which is opposite to the box.

$\endgroup$
  • $\begingroup$ Hello Sasi. It is an interesting way. Thanks a lot! $\endgroup$ – Freya Dec 14 '15 at 15:37
  • 1
    $\begingroup$ This construction doesn't give any reason why the resulting lines sum up to 15. And it doesn't say whether the solution is unique. It is at best a way to remember the solution. $\endgroup$ – Florian F Oct 26 '16 at 13:32
  • $\begingroup$ @FlorianF indeed and Also does this approach work for 4X4 ? $\endgroup$ – roottraveller May 10 '17 at 11:24
3
$\begingroup$

Another approach is to consider the ways to obtain 15 as a sum of 3 different numbers in the range 1..9.

These are: 1+5+9, 1+6+8, 2+4+9, 2+5+8, 2+6+7, 3+4+8, 3+5+7, 4+5+6.

As you can see there are only 8 ways, and you need 8 different sums in your square. So each sum appears exactly once as a line in your square.

5 must be in the center because it appears 4 times. Only the center belongs to 4 lines. 2,4,6,8 are corners because they appear 3 times each. 1,3,7,9 are borders because they appear only twice.

From there the solution is found easily. And it is unique.

$\endgroup$

protected by Community May 24 '15 at 14:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.