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:

6 1 8
7 5 3
2 9 4

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?

  • 2
    $\begingroup$ These are called "magic squares". $\endgroup$
    – Hovercouch
    Commented Jul 25, 2014 at 22:25
  • $\begingroup$ For the case of a three-by-three magic square, there is only one solution. $\endgroup$
    – user88
    Commented Jul 30, 2014 at 1:40
  • $\begingroup$ This question might just have the lowest "views:upvotes" ratio I have ever seen (2:56585) $\endgroup$ Commented Sep 22, 2015 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
    Commented Sep 22, 2015 at 18:29

5 Answers 5


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:

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

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.

  • $\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
    Commented Jul 31, 2014 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
    Commented Aug 1, 2014 at 12:36
  • $\begingroup$ @ Alan I see. It is a very nice way and I am glad to have been taught this. :) $\endgroup$
    – Freya
    Commented Aug 8, 2014 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
    Commented Dec 5, 2014 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$ Commented Jul 26, 2016 at 7:02

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....


It is simple. Put

1 2 3
4 5 6
7 8 9

…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.

  • $\begingroup$ Hello Sasi. It is an interesting way. Thanks a lot! $\endgroup$
    – Freya
    Commented Dec 14, 2015 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
    Commented Oct 26, 2016 at 13:32
  • $\begingroup$ @FlorianF indeed and Also does this approach work for 4X4 ? $\endgroup$ Commented May 10, 2017 at 11:24

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.

With 5 in the center 2 and 8 must be in opposite corners, so are 4 and 6. After that the odd number fit in a single place each.

So the solution is unique except for rotations and reflections. Which variation you get depends on how you arrange the corners.


The numbers from one to nine taken three at a time formulate 63 combinations. From these numbers 1,3,5,7,9 are odd and the rest 2,4,6,8 are even. The even numbers taken two at a time must be paired in a particular way 2,6 and 4,8. If paired 2,8 and 4,6 then the number 5 has to be used two times to make the rows equal to 15. So let's complete the two rows 8,3,4 and 6,7,2. The remaining numbers formulate the third row 1,5,9. Now we have the 3 rows.

8 3 4

1 5 9

6 7 2

All the columns and the rows sum to 15.

For unlimited solutions for the 3x3 grid which follows the rules set in the question you apply the method I used in my solution. If you multiply the number 9 by any natural number greater than zero then you obtain the next 3x3 grid.Let's have nx9: if n=2 then we have $\frac{(18X9)-(9X10)}{6}=42$ which is the sum for rows, columns, diagonals. We have 10,11,12,13,14,15,16,17,18. Now we have 5 even and 4 odd numbers. There is only one way to pair the odd numbers 13,17 and 11,15. Inserting the even numbers 12 and 16 we have 13,12,17 and 11,16,15. The remaining even numbers make the third row.

13 12 17

18 14 10

11 16 15

all add to 42.


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