A method for generating 3x3 and 4x4 magic squares


Consider the magic square as a sum of component matrices:
     (1 1 1)    (1  -1 0)    (0  1 -1)
M = A(1 1 1) + B(-1 0  1) + C(-1 0  1)
     (1 1 1)    (0  1 -1)    (1  -1 0)
Choosing different values of A, B and C produces a magic square every time. Substituting A=5, B=3 and C=-1 yields ariels' magic square:
(8 1 6)
(3 5 7)
(4 9 2)
A 4x4 magic square can be generated in a similar way:
     (1 1 1 1)    (1 0 0 1)    (1 0 1 0)    (1 1 0 0)    (1 0 0 1)
M = A(1 1 1 1) + B(0 1 1 0) + C(0 1 0 1) + D(0 0 1 1) + E(1 0 0 1)
     (1 1 1 1)    (1 0 0 1)    (0 1 0 1)    (0 0 1 1)    (0 1 1 0)
     (1 1 1 1)    (0 1 1 0)    (1 0 1 0)    (1 1 0 0)    (0 1 1 0)

To generate the one mentioned by jerkass, substitute A=1, B=8, C=2, D=1, E=4. It is no coincidence that B, C, D and E are powers of 2 - the binary number system generates consecutive integers in this case.