MAGIC - Magic Squares, Version 2 (6/21/88), Copyright (c) 1988 by
Gordon C. Everstine, 12020 Golden Twig Court, Gaithersburg, MD 20878
One mathematical amusement which has fascinated people for centuries
is the magic square, a square array (or matrix) of numbers having the
property that the sum of the entries in any row, column, or main diagonal
is always the same. For example, a magic square of order 6 is
31 2 3 34 5 36
30 26 9 10 29 7
24 23 16 15 14 19
13 17 22 21 20 18
12 8 28 27 11 25
1 35 33 4 32 6
For this array, the sum of any row, column, or main diagonal is 111.
Although magic squares are mathematical curies, their fascination has
perhaps resulted from the symmetry and order which they possess.
Normally, interest in magic squares is restricted to those formed
from the first N*N positive integers, where N is the order (or size) of
the square. The reason for this seemingly restricted interest is that
squares having negative or fractional numbers offer no additional
generality, because such squares can always be transformed into positive
integer squares by adding or multiplying every entry by a suitable
The IBM PC program which accompanies this text file computes magic
squares for any order square between 3 and 170. The upper limit of 170
is not a limitation of the algorithms, which are fully general, but of
the memory available in Basic, because the program stores the entire
square in an array. Squares of order 2 do not exist; order 1 is trivial.
Odd and even order squares must be constructed by different
approaches. For odd orders, the program uses a recursive scheme devised
by de la Loubere about 300 years ago. For even orders, the program uses
the Devedec algorithm. Even orders not divisible by 4 are formed
slightly differently from those which are divisible by 4.
The program displays magic squares of order up to 20 in the same way
that the example of order six was displayed above. For larger squares, a
row will not fit on one line of the screen, so that each row of the
square is displayed on several lines of the screen. Squares may also be
printed; for larger squares, fan-fold paper is required, since the
program prints the square in several blocks which can be arranged side-by-
side. All numbers are right-adjusted for ease of reading.
The correctness of any square may be optionally checked. This check
is primarily to verify that the program has not been corrupted during
copying or downloading.
The time required to calculate a magic square is roughly
proportional to N*N, the square of the order. However, this
implementation is efficient enough that most squares are computed
essentially instantaneously. A square of order 170 requires about four
seconds on my XT. The checking of a square takes longer than the
calculation, since sums must be accumulated in double precision.
For more information about magic squares, see, for example, "Magic
Squares and Cubes" by W.S. Andrews (The Open Court Publishing Co.,
Chicago, 1908) or "Mathematical Recreations" by M. Kraitchik (Dover,