Magic Squares

Challenge: Create a 3 by 3 grid using the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9 once only, so that each row, each column and each diagonal sums to 15. Such a grid is called a magic square. Here is a failed attempt:

 4 1 6 2 8 7 9 3 5

Note that the rows sum to 11, 17 and 17 ... . This square is not magic.

Why set the sums to 15? This is because the sum of the integers 1 through 9 is 45, so dividing by 3 (e.g. for the three rows) gives 15.

Let's solve this generally. Start with a generic square, letting a, b, c, ... , i stand in for the 9 integers.

 a b c d e f g h i

Thus, the three rows sum to 15:

a + b + c = 15
d + e + f = 15
g + h + i = 15

the three columns sum to 15:

a + d + g = 15
b + e + h = 15
c + f + i = 15

and the two diagonals sum to 15:

a + e + i = 15
c + e + g = 15

We have eight equations in nine variables. This is written as a system where the coefficients are shown as 0 or 1, just to show the overall structure a little more:

1a + 1b + 1c + 0d + 0e + 0f + 0g + 0h + 0i = 15
0a + 0b + 0c + 1d + 1e + 1f + 0g + 0h + 0i = 15
0a + 0b + 0c + 0d + 0e + 0f + 1g + 1h + 1i = 15
1a + 0b + 0c + 1d + 0e + 0f + 1g + 0h + 0i = 15
0a + 1b + 0c + 0d + 1e + 0f + 0g + 1h + 0i = 15
0a + 0b + 1c + 0d + 0e + 1f + 0g + 0h + 1i = 15
1a + 0b + 0c + 0d + 1e + 0f + 0g + 0h + 1i = 15
0a + 0b + 1c + 0d + 1e + 0f + 1g + 0h + 0i = 15

The equivalent system in reduced row echelon format is

1a + 0b + 0c + 0d + 0e + 0f + 0g + 0h + 1i = 10
0a + 1b + 0c + 0d + 0e + 0f + 0g + 1h + 0i = 10
0a + 0b + 1c + 0d + 0e + 0f + 0g − 1h − 1i = −5
0a + 0b + 0c + 1d + 0e + 0f + 0g − 1h − 2i = −10
0a + 0b + 0c + 0d + 1e + 0f + 0g + 0h + 0i = 5
0a + 0b + 0c + 0d + 0e + 1f + 0g + 1h + 2i = 20
0a + 0b + 0c + 0d + 0e + 0f + 1g + 1h + 1i = 15

Look carefully: there is one less row in the row-echelon form. This suggests that the eight restrictions are dependent, and that only seven are needed. In other words, once seven of the requirements are met, the eighth will follow automatically.

Note that h and i can be treated as parameter variables. Once values are chosen for h and i, the values for a ... g are then determined. We have the following equations:

 a = 10 − i b = 10 − h c = −5 + h + i d = −10 + h + 2i e = 5 f = 20 − h − 2i g = 15 − h − i

We create a table that shows all 81 possible pairs of h and i that could be selected to generate a square of values.

 h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8 h = 9 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9

We now shade those possibilities that are not permissible (in the sense that a choice for h and i would lead to a contradiction). For example, the fifth equation above declares that e = 5. Thus, neither h nor i can be 5. These cells are shaded:

 h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8 h = 9 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9

Also, h and i must be different integers, so hi. These cells are shaded:

 h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8 h = 9 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9

Since h and i lie in the bottom row, their sum cannot equal 10, since that would force g to be 5, which contradicts the fact that only e can be 5. Thus, we shade those cells in which h + i = 10:

 h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8 h = 9 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9

Also, h and i must be chosen so that its sum does not meet nor exceed 15, since that would force g to be 0 or a negative quantity. We now shade the cells in which h + i > 15. Also, the sum of h and i cannot be less than or equal to 5 since that would force g to be 10 or greater. We shade the cells in which h + i < 5:

 h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8 h = 9 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9

The expressions for elements d and f also provide more restrictions on h and i. Since d = −10 + h + 2i, and since d > 1, then −10 + h + 2i > 1, so that h + 2i > 11. Meanwhile, f = 20 − h − 2i, and since f > 1, then we have 20 − h − 2i > 1, or h + 2i < 19. The cells that violate these restrictions are then shaded:

 h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8 h = 9 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9

We try the remaining possible values for h and i and see what happens. For example, if h = 4 and i = 7, then this forces g = 4, which violates the rule that all entries must be different. The following is a list of initial values of h and i that cause a contradiction, causing two of the entries to take on the same value:

h = 4, i = 7: Causes g = 4.
h = 7, i = 4: Causes g = 4.
h = 9, i = 3: Causes g = 3.
h = 6, i = 3: Causes g = 6.
h = 3, i = 6: Causes g = 6.
h = 8, i = 3: Causes c = 6 and f = 6.
h = 8, i = 4: Causes a = 6 and d = 6.
h = 1, i = 7: Causes a = 3 and c = 3.
h = 2, i = 6: Causes a = 4 and d = 4.
h = 2, i = 7: Causes d = 6 and g = 6.

 h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8 h = 9 i = 1 i = 2 ☺ ☺ i = 3 i = 4 ☺ ☺ i = 5 i = 6 ☺ ☺ i = 7 i = 8 ☺ ☺ i = 9

This leaves eight possible parameter choices, marked by a ☺, that generate a magic square. Let's try h = 7, i = 2. Using the equations from above, we have

 a = 10 − 2 = 8 b = 10 − 7 = 3 c = −5 + 7 + 2 = 4 d = −10 + 7 + 2(2) = 1 e = 5 f = 20 − 7 − 2(2) = 9 g = 15 − 7 − 2 = 6

The resulting square is

 8 3 4 1 5 9 6 7 2

Each row, column and diagonal sum to 15. Try it. This is a magic square.

The fact that there are eight possible magic squares should not be surprising. Once we have found one square, then seven more can be generated by rotating the numbers a quarter-turn clockwise, a half-turn, and a three-quarters turn, or reflecting vertically or horizontally, or reflecting across the two diagonals. Thus, using the above square, the other squares are:

Quarter-turn clockwise:
 6 1 8 7 5 3 2 9 4
Half-turn clockwise:
 2 7 6 9 5 1 4 3 8
Three-quarters turn clockwise:
 4 9 2 3 5 7 8 1 6
Horizontal reflection:
 6 7 2 1 5 9 8 3 4
Vertical reflection:
 4 3 8 9 5 1 2 7 6
Reflection across one diagonal:
 8 1 6 3 5 7 4 9 2
Reflection across the other diagonal:
 2 9 4 7 5 3 6 1 8

Note that the values of h and i in the bottom row correspond to an unshaded cell in the table, thus indicating these will form a magic square.

Prepared by Scott Surgent. Report errors or questions to scott.surgent at gmail.com. (Oct 30, 2019, updated March 13, 2022)