# How do I solve these 3x3 magic squares? [duplicate]

I'm doing 3x3 magic squares. Here are the squares I'm working on:

|   | 5 |   |
|   |   |   |
| 8 |   |   |


The values must be between 3 and 12, and each line must add to 21.

Here's another one:

|   | 9 |   |
|   |   | 3 |
|   |   |   |


For this one, the boundaries are 3-11. As with the last one, each line total must add to 21.

• Magic squares are usually filled with distinct integers. When he says maximum he means that the range of integers is between those two numbers. 3-12 means that the minimum is 3 and the maximum is 12. May 11 '15 at 10:02
• I'm voting to close because the duplicated question I found offers the same techniques for an extremely similar puzzle, so I think it will be helpful for future readers to be directed to the other question. May 18 '15 at 0:28

In general, any $n\times n$ magic square of range [1, $n^2$] with odd number $n$ can be solved using the following algorithm:

1. Start at the middle grid in the bottom row. This is your 1.
2. Move downwards and to the right by one grid. If this move results in a position outside the square, wrap around to the beginning of the row (or column).
3. If 2 cannot be performed (i.e. the grid is already occupied or you are at the bottom-right corner), move upwards by one grid instead.
4. Repeat 2 (or 3) until all numbers are filled.

Illustration using $n=3$. (For cells 2 and 3 the position before wrapping is shown in parentheses.)

|   |   |   |
|   |   |   |
|   | 1 |   | // start here

|   |   | 2 |
|   |   |   |
|   | 1 |   | // move to the right and then down.
(2)  // Because moving down brings us outside the square,
// we wrap around to the start of the column

|   |   | 2 |
| 3 |   |   | (3)
|   | 1 |   | // move to the right and down.
// moving right brings us outside, so wrap around to start of row

| 4 |   | 2 |
| 3 |   |   |
|   | 1 |   | // cannot move down and right because that is occupied
// so we move up instead

| 4 |   | 2 |
| 3 | 5 |   |
|   | 1 |   | // move down and right

| 4 |   | 2 |
| 3 | 5 |   |
|   | 1 | 6 | // move down and right

| 4 |   | 2 |
| 3 | 5 | 7 |
|   | 1 | 6 | // cannot move down and right, so move up

| 4 |   | 2 |
| 3 | 5 | 7 |
| 8 | 1 | 6 | // move down and right, wrap around to start of row

| 4 | 9 | 2 |
| 3 | 5 | 7 |
| 8 | 1 | 6 | // move down and right, wrap around to start of column


Now, if you need to solve your magic square that starts with 3, simply add 2 to all cells of this standard square. Then rotate and/or reflect it until you get one where the numbers match your given ones.

For example, consider your first square (assuming we can omit 12):

|   | 5 |   |
|   |   |   |
| 8 |   |   |


We first add 2 to all elements of our standard square:

| 6  | 11 | 4  |
| 5  | 7  | 9  |
| 10 | 3  | 8  |


Then rotate clockwise by 90 degrees:

| 10 | 5  | 6  |
| 3  | 7  | 11 |
| 8  | 9  | 4  |


Further reading: Magic Square from Wolfram MathWorld, which includes methods for solving even squares as well.

• Can you expand your answer to include the specific situations given in the question? May 11 '15 at 10:33
• @JasonLepack Done with the first square. Since it is the same method for the second, and other people have already posted answers, I'll leave that out. May 11 '15 at 10:38
• This generates an $n\times n$ square, and fortunately it's unique for the $3\times 3$ case, but it's not unique in general; I wouldn't be surprised if the abstract problem here (given some subset of set cells and an interval, is there a possibly unique $n\times n$ magic square that matches the given values on that set?) is actually computationally hard. May 12 '15 at 3:36

Here is an alternative technique. Consider your first magic square:

| a | 5 | b |
| c | d | e |
| 8 | f | g |


I filled in the empty cells with the variables $a$ through $g$. We will create a system of equations to solve for these seven variables.

We know that each row must sum to 21. As a result, we can write the following three equations:

• 1st row: $a + 5 + b = 21$
• 2nd row: $c + d + e = 21$
• 3rd row: $8 + f + g = 21$

Likewise, every column and diagonal must sum to 21, giving us five more equations:

• 1st column: $a + c + 8 = 21$
• 2nd column: $5 + d + f = 21$
• 3rd column: $b + e + g = 21$
• \ diagonal: $a + d + g = 21$
• / diagonal: $8 + d + b = 21$

So we now have 8 equations with 7 unknowns. We can select any seven of them and apply the usual equation-solving techniques to find the values of $a$, $b$, $c$, $d$, $e$, $f$, and $g$, giving us the solution to the magic square.

The answer for the first puzzle is this:

|10 | 5 | 6 |
| 3 | 7 |11 |
| 8 | 9 | 4 |

| A | B | C |
| D | E | F |
| G | H | I |

| A | 5 | C |
| D | E | F |
| 8 | H | I |


Range 3 - 12.

There is one extra integer.

Neither 11 or 12 can be in the same row, column or diagonal as 8 since the other number would be 1 or 2 which is outside the range. Therefore F is 11 or 12 and the other is the integer that is not included.

Neither 3 nor 4 can be in the same row or column as 5 since the other number would be 12 or 13 and 13 is outside the range and 12 must be in F if it is in the puzzle. Therefore D and I are 3 and 4. Continuing H and A are 10 and 9 and E and C are 6 and 7.

I cannot be 3 since either E would be 6 or 7 and A would be 11 or 12 and 11 and 12 is in F or not in the puzzle. Therfore I = 4. The rest of the puzzle fills itself in from there. The number not included is 12.

For the 1st question:

| 10 | 5 | 6 |

| 3 | 7 | 11 |

| 8 | 9 | 4 |

And Jimmy has given the answer for the 2nd question

For the method. Its very simple. Everyone knows how to solve 3*3 to get 15. But we need 21 i.e is 2 more for every cell. so add 2 in every cell of magic square. that will make 1 as 3 and 9 as 11. And you will then have to just manage the rows.

• The question asks 'How do I solve these 3x3 magic squares?', not 'Solve these 3x3 magic squares'. Your answer needs an explanation. May 11 '15 at 11:21