Skip to content

magic squares

A simple analysis of magic squares

Donal Moore and Stephen Lavelle

Everyone knows magic squares right? A square grid of numbers where the sums of the numbers along each of the rows and columns and both the diagonals are all equal. (there are proper books about this etc., so google if you want to find out more).

e.g.:

8 5 5
3 6 9
7 7 4

The sum along any row or column or diagonal is 18.

Let’s represent the magic square mathematically.

a b c
d e f
g h i

And let the sum of each row/column be equal to n

Now we have the following equations:

  1. a+b+c=n
  2. d+e+f=n
  3. g+h+i=n
  4. a+d+g=n
  5. b+e+h=n
  6. c+f+i=n
  7. a+e+i=n
  8. c+e+g=n

Now, because all the equations equal n, we can put any left side equal to any other left side. Let’s combine the 1st one with the 7th one: we now have:
a+b+c=g+e+c
=>a+b=g+e

similarly,
a+g=e+f
a=e+f-g

So we can substitute this into the former equation:
e+f-g=g+e
e+f=2g

Which, we concluded, was quite a neat thing. You can also easilly find that a similar thing holds for the other corners, so all magic squares have to be of the form:

(y+z)/2 w (x+z)/2
x ? y
(w+y)/2 z (w+x)/2

Now, because we now know the value of one of the rows we can easily find the value of the ? in the center.

(y+z)/2+x+(w+y)/2 = w+k+z
y+z+2x+w+y=2w+2k+2z
2(x+y)-(w+z)=w2

but x+y=w+z

so w=(x+y)/2

which i think is pretty cool :)

So, we have

(y+z)/2 w (x+z)/2
x (x+y)/2 y
(w+y)/2 z (w+x)/2

But we can restrict if further, because for (y+z)/2 to be a whole number that means that y+z must be even, which means that y and z have to have even parity (either all even or all odd).

And we actually only need three numbers because:
x+w=y+z
=>z=x+w-y

which gives:

(x+w)/2 w (2x+w-y)/2
x (x+y)/2 y
(w+y)/2 x+w-y (w+x)/2

So we need only three numbers of similar parity then. But there’s 1 further thing(using the variable names below) if we want to restrict ourselves to having only positive values in the table:

a b c
d e f
g h i

If h+e>=a+e+f then w would have to be non-positive. Therefore
h+e<a+e+f.

Similarly you can show that any of {b,d,f,h} have to be greater than any of {a+i,c+g} :)

So, they’re the constraints we’ve managed to work out so far – we’re looking at trying to make all the numbers different – but that’s pretty hard to do.

Exactly the same method can be used to find out how to construct a 4×4, or 5×5, or NxN magic square, but the calculations are AWFUL to carry out by hand, and very quickly you find that the number of free variables begins to outnumber the number of dependant ones. Anyway, here’s the structure of 4×4 and 5×5 magic squares:

h+l-m h-j+k+l-m-n+p j-h-k-l+2m+n m+n+o-h-l
j+k-h 2m+n+o-h-k-l h-j+l-m+p h
m+n+o+p-j-k-l j k l
m n o p

Notice that for the one above, for any integers h,j,k,l,m,n,o,p, the whole table contains just integers (to put them all positive takes an awful amount of constraints-not really worth it)

And the 5×5:

(h+2j+l-m+n+2o+r+2s-3t-u-v-w-x)/2 (h+2j-l+m+n+2o-2q+3r+2s-3t-3u-v-w+x)/2 -h-m-r+t+u+w+x -j+m-n-o+q-r-s+2t+u+v -j-o-s+t+u+v+w
(-h-2j+l+3m+n+2q+3r-t-u-v-w-x)/2 (-h-2j-l-m-n-2o-3r-2s+5t+3u+3v+3w+x)/2 h j-m+o-q+s-t+x j
-l-m-n-o+t+u+v+w+x l m n o
-q-2r-s+t+u+v+w+x q r s t
u v w x y

It’s awful, the formula don’t ya think?

I found out these by using the following mathematica code (the 3×3 code’s there as well, though it gives a different version of the formula to mine):

Solve[{
    	a + b + c == x,
    	d + e + f == x,
    	g + h + i == x,
    	a + d + g == x,
    	b + e + h == x,
    	c + f + i == x,
    	a + e + i == x,
    	c + e + g == x
    }]

Solve[{
    	a + b + c + d == x,
    	e + f + g + h == x,
    	i + j + k + l == x,
    	m + n + o + p == x,
    	a + e + i + m == x,
    	b + f + j + n == x,
    	c + g + k + o == x,
    	d + h + l + p == x,
    	a + f + k + p == x,
    	d + g + j + m == x

    }]]

FullSimplify[Solve[{
      a + b + c + d + e == z,
      f + g + h + i + j == z,
      k + l + m + n + o == z,
      p + q + r + r + s == z,
      t + u + v + w + x == z,

      a + f + k + p + t == z,
      b + g + l + q + u == z,
      c + h + m + r + v == z,
      d + i + n + r + w == z,
      e + j + o + s + x == z,
      a + g + m + r + x == z,
      e + i + m + q + t == z
      }]

yeah, so sue me for using mathematica – the alternative was to program it the matrix way, and i really don’t have time for that right now.

Just because it’s relevant, the any of the above systems of linear equations can be written in matrix form, so all you have to do is solve it – :D

That’s why i like computers

=S=

One Comment

  1. Ethan wrote:

    ethan
    I found these hard and then I found them easy I learnt how to do them.

    age 7

    Wednesday, June 13, 2007 at 6:11 pm | Permalink