Magic Squares

The sum of the numbers in each row, column and main diagonal is the same. It is called the magic constant. In the applet below, magic squares of order (size) 3, 5, 7 and 9 may be explored. A magic square of odd order n contains the integers from 1 to n2, arranged so that the rows, columns and diagonals sum to the magic constant (n (n2 + 1)) / 2. The middle number in each square is (n2 + 1)/2. Read more about the squares and the applet here.



applet-java must be enabled   Applet Controls

  Reset : reset to initial square
  R0 : rotate 0 degrees clockwise
  R1 : rotate 90 degrees clockwise
  R2 : rotate 180 degrees clockwise
  R3 : rotate 270 degrees clockwise
  FH : flip horizontally
  FV : flip vertically
  FX+ : flip on the line y = x
  FX- : flip on the line y = -x
  Order : select the size of the square

The Applet

The buttons in the applet apply a set of motions to the square which is known in group theory as the dihedral group D4. The motions are all rotations and reflections of the square in the Euclidean plane, which are rigid in the sense that they do not bend, tear or stretch the square. These rotations and reflections preserve the magic quality of a magic square.

To construct a magic square, the "diagonal" method is used which is based on a method introduced in Europe in 1693 by De la Loubere, who learned it while he was envoy to Siam. Magic squares are mentioned in Chinese mythology, which claims the oldest magic square, Lo Shu, appeared on a turtle's back in the river Lo in 2200 B.C.. The Arabian mathematician Tabit ibn Korra wrote about magic squares in the ninth century. Five hundred years later, the squares became known in Europe. Scientists such as Cardan, Kepler, and Fermat were interested in them, as well as medieval astrologers and physicians who ascribed mystical properties to them. The eighteenth century West African astronomer and mathematician Muhammad ibn Muhammad wrote a detailed manuscript in 1732 which provides several formulas for the construction of odd-order magic squares. In his manuscript, examples of the squares are intentionally defective, requiring the serious student to calculate the square correctly.




//Java code for generating magic squares using the diagonal method:

//some variables
int i, j, x, y, xNew, yNew, n=ORDER_OF_MAGICSQUARE;
int nSquared=n*n;

//the square is a 2-D array of integers 
int mSquare[][]=new int[n][n];

//set all values in the square to zero
for (i=0;i<n;i++)
	for (j=0;j<n;j++)
		mSquare[i][j]=0;
      
//set position (x,y) to the center of the first row
x=0;
y=n/2;      

//"magically" position the integers from 1 to n squared into the square
for (i=0;i<nSquared;i++) {

	//set the value of the square at (x,y)
	mSquare[x][y]=i+1;

	//Move (x,y) to a new position in an upper-left diagonal manner. 
	//The values wrap around modulo n to prevent them from going 
	//outside the square. The modulus operator % requires a positive 
	//left operand, so add n to x-1 and to y-1.
	xNew=(x-1+n) % n;
	yNew=(y-1+n) % n;

	if (mSquare[xNew][yNew]==0) {
		//the new position is empty, it may be used
		x=xNew;
		y=yNew;
	}
	else{
		//the new position already contains a value, set
		//it to the adjacent right instead of the upper left
		x=(x+1+n) % n;
        } 
}