MAGIC

Purpose:

Creates an NxN magic square.

Syntax:

MAGIC(n)

 

n   -   An integer. The number of output rows and columns.

Returns:

A square matrix.

Example:

a = magic(3)

 

a == {{8, 1, 6},

      {3, 5, 7}, 

      {4, 9, 2}} 

 

 

colsum(a) == {{15, 15, 15}}

colsum(a) == {{15, 15, 15}}

sum(diag(a)) == 15

 

Example:

density(magic(122));rainbow

 

image\magicpic.gif

 

produces an interesting density plot.

Remarks:

A magic square is a square matrix where the sum of each row equals the sum of each column and also equals the sum of the main diagonal.

 

MAGIC(2) does not produce a true magic square since a 2x2 magic square does not exist.

 

For n <= 0, MAGIC returns the empty matrix.

See Also:

COLSUM

DIAGONAL