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 x_{1}, x_{2}, x_{3}, ... , x_{9} stand in for the 9 integers.
Thus, the three rows sum to 15:
x_{4} + x_{5} + x_{6} = 15 x_{7} + x_{8} + x_{9} = 15 the three columns sum to 15:
x_{2} + x_{5} + x_{8} = 15 x_{3} + x_{6} + x_{9} = 15 and the two diagonals sum to 15:
x_{7} + x_{5} + x_{3} = 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:
0x_{1} + 0x_{2} + 0x_{3} + 1x_{4} + 1x_{5} + 1x_{6} + 0x_{7} + 0x_{8} + 0x_{9} = 15 0x_{1} + 0x_{2} + 0x_{3} + 0x_{4} + 0x_{5} + 0x_{6} + 1x_{7} + 1x_{8} + 1x_{9} = 15 1x_{1} + 0x_{2} + 0x_{3} + 1x_{4} + 0x_{5} + 0x_{6} + 1x_{7} + 0x_{8} + 0x_{9} = 15 0x_{1} + 1x_{2} + 0x_{3} + 0x_{4} + 1x_{5} + 0x_{6} + 0x_{7} + 1x_{8} + 0x_{9} = 15 0x_{1} + 0x_{2} + 1x_{3} + 0x_{4} + 0x_{5} + 1x_{6} + 0x_{7} + 0x_{8} + 1x_{9} = 15 1x_{1} + 0x_{2} + 0x_{3} + 0x_{4} + 1x_{5} + 0x_{6} + 0x_{7} + 0x_{8} + 1x_{9} = 15 0x_{1} + 0x_{2} + 1x_{3} + 0x_{4} + 1x_{5} + 0x_{6} + 1x_{7} + 0x_{8} + 0x_{9} = 15 The equivalent system in reduced row echelon format is
0x_{1} + 1x_{2} + 0x_{3} + 0x_{4} + 0x_{5} + 0x_{6} + 0x_{7} + 1x_{8} + 0x_{9} = 10 0x_{1} + 0x_{2} + 1x_{3} + 0x_{4} + 0x_{5} + 0x_{6} + 0x_{7} − 1x_{8} − 1x_{9} = 5 0x_{1} + 0x_{2} + 0x_{3} + 1x_{4} + 0x_{5} + 0x_{6} + 0x_{7} − 1x_{8} − 2x_{9} = 10 0x_{1} + 0x_{2} + 0x_{3} + 0x_{4} + 1x_{5} + 0x_{6} + 0x_{7} + 0x_{8} + 0x_{9} = 5 0x_{1} + 0x_{2} + 0x_{3} + 0x_{4} + 0x_{5} + 1x_{6} + 0x_{7} + 1x_{8} + 2x_{9} = 20 0x_{1} + 0x_{2} + 0x_{3} + 0x_{4} + 0x_{5} + 0x_{6} + 1x_{7} + 1x_{8} + 1x_{9} = 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 x_{8} and x_{9} can be treated as parameter variables. Once values are chosen for x_{8} and x_{9}, the values for x_{1} ... x_{7} are then determined. We have the following equations:
x_{2} = 10 − x_{8} x_{3} = 5 + x_{8} + x_{9} x_{4} = 10 + x_{8} + 2x_{9} x_{5} = 5 x_{6} = 20 − x_{8} − 2x_{9} x_{7} = 15 − x_{8} − x_{9} We create a table that shows all 81 possible pairs of x_{8} and x_{9} 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 x_{8} and x_{9} would lead to a contradiction). For example, the fifth equation above declares that x_{5} = 5. Thus, neither x_{8} nor x_{9} can be 5. These cells are shaded:
Also, x_{8} and x_{9} must be different integers, so x_{8} ≠ x_{9}. These cells are shaded:
Since x_{8} and x_{9} lie in the bottom row, their sum cannot equal 10, since that would force x_{7} to be 5, which contradicts the fact that only x_{5} can be 5. Thus, we shade those cells in which x_{8} + x_{9} = 10:
Also, x_{8} and x_{9} must be chosen so that its sum does not meet nor exceed 15, since that would force x_{7} to be 0 or a negative quantity. We now shade the cells in which x_{8} + x_{9} > 15. Also, the sum of x_{8} and x_{9} cannot be less than or equal to 5 since that would force x_{7} to be 10 or greater. We shade the cells in which x_{8} + x_{9} < 5:
The expressions for elements x_{4} and x_{6} also provide more restrictions on x_{8} and x_{9}. Since x_{4} = 10 + x_{8} + 2x_{9}, and since x_{4} > 1, then 10 + x_{8} + 2x_{9} > 1, so that x_{8} + 2x_{9} > 11. Meanwhile, x_{6} = 20 − x_{8} − 2x_{9}, and since x_{6} > 1, then we have 20 − x_{8} − 2x_{9} > 1, or x_{8} + 2x_{9} < 19. The cells that violate these restrictions are then shaded:
So far, we have been able to delete 63 possibilities. We can now try some of the remaining possible values for x_{8} and x_{9} and see what happens. For example, if x_{8} = 4 and x_{9} = 7, then this forces x_{7} = 4, which violates the rule that all entries must be different. The following is a list of initial values of x_{8} and x_{9} that cause a contradiction, causing two of the entries to take on the same value:
x_{8} = 7, x_{9} = 4: Causes x_{7} = 4. x_{8} = 9, x_{9} = 3: Causes x_{7} = 3. x_{8} = 6, x_{9} = 3: Causes x_{7} = 6. x_{8} = 3, x_{9} = 6: Causes x_{7} = 6. x_{8} = 8, x_{9} = 3: Causes x_{3} = 6 and x_{6} = 6. x_{8} = 8, x_{9} = 4: Causes x_{1} = 6 and x_{4} = 6. x_{8} = 1, x_{9} = 7: Causes x_{1} = 3 and x_{3} = 3. x_{8} = 2, x_{9} = 6: Causes x_{1} = 4 and x_{4} = 4. x_{8} = 2, x_{9} = 7: Causes x_{4} = 6 and x_{7} = 6. These contradictions are shaded:
This leaves eight possible parameter choices, marked by a 🌝, that generate a magic square. Let's try x_{8} = 7, x_{9} = 2. Thus, x_{1} = 8, x_{2} = 3, x_{3} = 4, x_{4} = 1, x_{5} = 5, x_{6} = 9 and x_{7} = 6. 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 x_{8} and x_{9} 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 surgent@asu.edu. (Oct 30, 2019, updated Feb 16, 2021)
