Magic Squares for Odd, Singly Even, and Doubly Even Orders

Requires a Wolfram Notebook System

Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

A magic square is a square array of the numbers 1, 2, …, arranged so that the sum of the numbers in a row, column, or either main diagonal is constant. This Demonstration shows magic squares of any order up to 14. Different algorithms are used to generate the magic squares, depending on whether the order is odd, singly even, or doubly even (see Details).

Contributed by: Shutao Tang (May 2015)
(Northwestern Polytechnical University, Xi'an City, China)
Open content licensed under CC BY-NC-SA


Snapshots


Details

Define

,

.

Then the magic square is

.

Define

1. ,

2. ,

3. ; here, "" is the equal operation yielding True or False component-wise, so that is a matrix of Boolean values.

Define to be a matrix of the integers from 1 to , stored sequentially in rows.

The elements in where is False are left alone, and the elements in where is True are reversed; that is, the value at a True position is replaced with .

For example, let

1.

2.

3.

The elements in where is False are left alone.

The elements in where is True are reversed; that is, at such a position with value , the new value is .

So this is final result.

If is singly even, then is odd, and a magic square of order can be constructed from four copies of the magic square of order .

Construct a matrix with four blocks:

,

where is a magic square of order , , , and .

Since is odd, for some . Suppose

.

1. Swap the values of at , , and with the corresponding values of .

Suppose

.

2. Swap the values of at with the corresponding values of .

For instance, let , so .

Then the block matrix (with the first swap colored) is

.

1. Swap the values of at and with the corresponding values of . (Now the second swap is colored.)

2. Swap the values of at with the corresponding values of .

References

[1] C. Moler, Experiments with MATLAB, MathWorks, Inc., 2011 pp. 129–132. www.mathworks.com/moler/exm.

[2] kguler. "Answer to 'Better Method to Swap the Values of Two 2-D Arrays'." Mathematica Stack Exchange. (Apr 23, 2015) mathematica.stackexchange.com/questions/80674/better-method-to-swap-the-values-of-two-2-d-arrays/80680#80680.



Feedback (field required)
Email (field required) Name
Occupation Organization
Note: Your message & contact information may be shared with the author of any specific Demonstration for which you give feedback.
Send