From: goddard@NeXTwork.Rose-Hulman.Edu (Bart Goddard)
Subject: Re: Magic squares placement algorithm?
Organization: Rose-Hulman Institute of Technology
Date: Wed, 12 Aug 1992 15:21:59 GMT


In article <1992Aug12.094525.12675@phillip.edu.au>  
x01233@phillip.edu.au writes:

> I once used, years ago, an algorithm for creating magic squares (i.e.  
a square
> full of numbers, all rows and all columns add up to the same number).
> The algorithm stated where to put each and every number in the  
matrix.
> 
> Sadly I have lost this algorithm... Does anyone know what it is?
> 
There is an algorithm for odd-order magic squares:

The general idea is to write the numbers in order, always moving  
diagonally up-right.  The snag is that one crashes into the sides of  
the squares or into squares that already have numbers in them, and one  
needs rules to handle these cases.

1.  If the next box would cross the right side, go to the left-most box  
of the row above (which will always be empty!)

2.  If the next box would cross the top side, go to the lowest box of  
the column to the right.

3.  If the next box is filled, go to the box directly below the CURRENT
box (not the box below the "next" box).

4.  When you hit the upper right corner, go down one (apply rule 3.)

5.  Start with 1 (or any integer for that matter) in the middle box in  
the top row.

Confused?
Here's the 5x5:
17 24  1  8 15
23  5  7 14 16
 4  6 13 20 22
10 12 19 21  3
11 18 25  2  9

I put 1 in the center top (rule 5).  I'd LIKE to put 2 in the box  
up-right, but there isn't one, so I apply rule 2, and put it in the  
lowest box in the next column.  The box up-right from the 2 is empty,  
so the 3 goes there.
But now I've hit the right side, so the 4 goes in the left-most box of
the row above (rule 1).  The 5 goes in the up-right box from the 4.   
Now I've hit the box that has the 1 in it (rule 3), so the 6 goes in  
below the 5.  7 and 8 follow nicely.  I've hit the top, so the 9 goes  
in the bottom of the next column. Now I've hit the side, so the 10 goes  
up and way to the left.  The next box has the 6 in it already, so the  
11 goes below the 10. 12-15 go in nicely, and then I use rule 4 and put  
16 below the 15.  I think that's an example of everything that can  
happen.

The only one I know for even-order squares is for the 4x4 case, where  
the numbers are written in order and the diagonals flipped:
 1  2  3  4                16  2  3 13
 5  6  7  8                 5 11 10  8
 9 10 11 12    --------->   9  7  6 12
13 14 15 16                 4  14 15 1

Further, if one constructs a magic square of magic squares, one gets a  
magic square.  So I could construct a 12x12 magic square by the  
following method:
Make a 3x3 magic square using the algorithm above and the numbers 1-9,  
and call this MS1.  Make a second 3x3 using the numbers 10-18. This is  
MS2.
MS3 uses 19-27 and so on, until MS16.  Then arrange these 16 3x3  
according to the 4x4 pattern above (MSj goes in the box with j in it.)

In elementary school I won a local math contest by constructing an  
81x81 magic square which was really a 3x3 of 27x27's which in turn were  
3x3's of
9x9's which were 3x3's of 3x3's.  (The tedium wasn't worth the 5 bucks  
I got for first prize.)

Hope this helps.
Bart Goddard

Newsgroups: rec.puzzles