offside flag

Something Magical about Squares

Well, not really. The following are just a few ways to construct a magic square.

So what is a magic square? A magic square of order n is a grid of n rows and n columns where the sum of the entries in any row, column, or diagonal is the same (usually there is an additional restriction where we are only allowed to use the numbers 1, ... n2 and no two entries of the square can have the same number).


Magic Squares of Odd Order

We start with an n by n grid (n is odd). Then fill the entries of the middle row (or the (n+1)/2-th row) of the grid with the following:

1 n + 2 2n + 3 3n + 4 ... n2

(i.e. the (j+1)-th entry= the j-th entry + (n + 1))

Then fill out the entries as follows:
If the entry in the i-th row, j-th column is x, then the entry in the (i+1)-th row, (j+1)-th column is x + 2n. If x + 2n is greater than n2, then subtract x + 2n by n2. If the i-th row is the final row, consider the first row of the grid as the (i+1)-th row, likewise for columns.

For example, for n=5, we have:

18 24 5 6 12
22 3 9 15 16
1 7 13 19 25
10 11 17 23 4
14 20 21 2 8

This method generates a magic square of order n for any odd number n. (In fact, we can replace the 2 in the expression x + 2n by any number m, m ≠ 1, that is co-prime with n to obtain a different magic square.)


Magic Squares whose order is even, but not divisible by 4

For n that is even but not divisible by 4, we can use a different method to construct a magic square of order n.

There is no magic square of order 2.

First we express n as 4m+2, where m is a positive integer. (Since n is even and not divisible by 4, n divided by 4 gives a remainder of 2.)

We start with an empty n-by-n grid and we use the symbol Si,j to denote the entry on the i-th row, j-th column. (S1, 1 is the entry at the top-left corner of the grid.)

We first fill the entries the diagonals. We have Si, i = i × n - (i - 1) and
Si, (n - i + 1) = (i - 1) × n + i.

For example, for n = 10, we have the following:

10 1
19 12
28 23
37 34
46 45
56 55
67 64
78 73
89 82
100 91

We then fill the middle 2 columns of the grid as follows:

So, for n = 10 (= 2 × 4 + 2, so m = 2), we have the following:

10 5 96 1
19 85 86 12
28 75 76 23
37 65 66 34
46 45
56 55
67 36 35 64
78 26 25 73
89 16 15 82
100 95 6 91

Now we fill the rest of entries in the first and final columns as follows:

So, for n = 10, we have the following:

10 5 96 1
11 19 85 86 12 90
80 28 75 76 23 21
31 37 65 66 34 70
51 46 45 60
50 56 55 41
61 67 36 35 64 40
30 78 26 25 73 71
81 89 16 15 82 20
100 95 6 91

Next we deal with the middle 2 rows. We fill the blank entries as follows:

So for n = 10, the middle 2 rows look like this:

51 42 43 44 46 45 57 58 59 60
50 52 53 54 56 55 47 48 49 41

We fill the rest of the entries using a method called "Large-Small-Large Rule" (I have yet to come up with a better name). The method goes like this:

  1. For each row i, i = 1, 2, ... 2m, we find the smallest j where the i,j-th entry of the grid is empty. Then we set
    Si, j = (n - i) × n + j. (Large)
  2. Find the next empty entry on row i and set Si, j = (i - 1) × n + j, where j is the column number. (Small)
  3. Find the next empty entry on row i and set Si, j = (n - i) × n + j, where j is the column number. (Large)
  4. We continue this pattern until the 2m-th column is filled. (The (2m + 1)-th and (2m + 2)-th columns on row i are already filled.)
  5. Si, 2m + 3 = Si, 2m + 3
  6. We continue to alternate between small and large values until we fill out the row.
  7. For i = 2m + 3, 2m + 4, ... n, if the (i,j)-th entry is empty, then Si, j = n2 + 1 - Sn + 1 - i, j

For example, in the second row of the 10-by-10 grid, the first empty entry is on the 3rd column, so S2, 3 = (10 - 2) × 10 + 3 = 83.

Then next empty entry is on the 4th column, so S2, 4 = (2 - 1) × 10 + 4 = 14. S2, 7 = S2, 4 + 3 = 14 + 3 = 17 and S2, 8 = 88.

We don't need to go any further as the row is now filled. The row would look as follows:

11 19 83 14 85 86 17 88 12 90

After the rest of the entries are filled, we have the following:

10 92 3 94 5 96 97 8 99 1
11 19 83 14 85 86 17 88 12 90
80 72 28 24 75 76 27 23 79 21
31 62 33 37 65 66 34 38 69 70
51 42 43 44 46 45 57 58 59 60
50 52 53 54 56 55 47 48 49 41
61 39 68 67 36 35 64 63 32 40
30 29 78 77 26 25 74 73 22 71
81 89 18 87 16 15 84 13 82 20
100 9 98 7 95 6 4 93 2 91

We can construct a different magic square by switching the words "rows" and "columns" in the above description.


Magic Squares whose order is divisible by 4

If n is divisible by 4, then we can construct a magic square of order n as follows:

  1. We express n in the form: n = 2ab, where a < 1 (since n is a multiple of 4) and b is odd (b may be equal to 1).
  2. Construct a magic square of order b (using any method).
  3. If a is even, then replace each entry of the magic square with the magic square
    1+i 15+i 8+i 10+i
    12+i 6+i 13+i 3+i
    14+i 4+i 11+i 5+i
    7+i 9+i 2+i 16+i
    where i = (the current entry of the magic square - 1) × 16
    This yields a magic square of order 4b.
    If a is odd, then we replace each entry of the magic square with the following:
    1+i 27+i 13+i 23+i 48+i 54+i 36+i 58+i
    20+i 10+i 32+i 6+i 61+i 39+i 49+i 43+i
    34+i 60+i 46+i 56+i 15+i 21+i 3+i 25+i
    51+i 41+i 63+i 37+i 30+i 8+i 18+i 12+i
    62+i 40+i 50+i 44+i 19+i 9+i 31+i 5+i
    47+i 53+i 35+i 57+i 2+i 28+i 14+i 24+i
    29+i 7+i 17+i 11+i 53+i 42+i 64+i 38+i
    16+i 22+i 4+i 26+i 33+i 59+i 45+i 55+i
    where i = (the current entry of the magic square - 1) × 64
    This yields a magic square of order 8b.
  4. We now continue the process (by expanding a magic square of order m into a magic square of order 4m) until we get a magic square of order n.

For example, we can use the following magic square of order 3 to construct a magic square of order 12.

8 3 4
1 5 9
6 7 2

The resulting magic square of order 12 is:

113 127 120 122 33 47 40 42 49 63 56 58
124 118 125 115 44 38 45 35 60 54 61 51
126 116 123 117 46 36 43 37 62 52 59 53
119 121 114 128 39 41 34 48 55 57 50 64
1 15 8 10 65 79 72 74 129 143 136 138
12 6 13 3 76 70 77 67 140 134 141 131
14 4 11 5 78 68 75 69 142 132 139 133
7 9 2 16 71 73 66 80 135 137 130 144
81 95 88 90 97 111 104 106 17 31 24 26
92 86 93 83 108 102 109 99 28 22 29 19
94 84 91 85 110 100 107 101 30 20 27 21
87 89 82 96 103 105 98 112 23 25 18 32

In general, one can use any magic square of two arbitrary orders, say a,b, to construct a magic square of order a×b using this method.


References and Acknowlegements

Eric W. Weisstein. "Magic Square." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/MagicSquare.html

The method on constructing magic squares of odd order shown on this page was discovered by J. H. Conway, even though I arrived at this method independently.