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:
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.
Thus, the three rows sum to 15:
d + e + f = 15 g + h + i = 15 the three columns sum to 15:
b + e + h = 15 c + f + i = 15 and the two diagonals sum to 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:
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
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 rowechelon 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:
We create a table that shows all 81 possible pairs of h and i that could be selected to generate a square of values.
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:
Also, h and i must be different integers, so h ≠ i. These cells are shaded:
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:
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:
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:
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 = 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. These contradictions are shaded:
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
The resulting square is
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 quarterturn clockwise, a halfturn, and a threequarters turn, or reflecting vertically or horizontally, or reflecting across the two diagonals. Thus, using the above square, the other squares are:
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)
