| home | contents | send comment | send link | add bookmark |

Operations on magic squares

by Stephen R. Schmitt

Input:
A size:
B size:
C size:
D size:

Contents

  1. About
  2. The source code
  3. Discussion

1. About

This Java Script calculator demonstrates the operations that can be performed on magic squares. Enter an integer at Input: and press Push to add a magic square to the top of the magic square stack. The operation Pop removes the topmost magic square from the stack. The multiply operation, A := A×B requires two magic squares; it combines the two magic squares at the top of the stack. The other operations transform the magic square at the top of the stack. The dimension of each magic square in the stack is shown in the windows below the input window. After each operation is completed, the contents of the top of the stack are written in a separate window. On invalid entries, a popup window will display an error message.

Return to Contents


2. The source code

The Java Script source code for this program can be viewed by using the View|Source command of your web browser.

You may use or modify this source code in any way you find useful, provided that you agree that the author has no warranty, obligations or liability. You must determine the suitability of this source code for your use.

Return to Contents


3. Discussion

Definition

A magic square is an arrangement of the numbers from 1 . . . N 2 in an N×N matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or either main diagonal is equal to the magic constant defined as:

 M2 N(N 2 + 1)
 2 

The simplest magic square is the  1×1  magic square whose only entry is the number 1. The next simplest is the  3×3  magic square:

8 1 6
3 5 7
4 9 2

and those derived from it by transformations.

Transformations

If every number in a magic square is subtracted from  N 2 + 1,  the complementary magic square is created. The complement to the magic square above is:

2 9 4
7 5 3
6 1 8

Other possible operations include rotation left or right, reversal to create a mirror image, inversion or flipping so the top row is on the bottom, and transposition where rows and columns are exchanged. These operations due not change the sums of the rows, columns, and diagonals and do not create distinctly new magic squares.

Multiplication

Addition of magic squares does not result in another magic square, so they cannot be added. However, they can be multiplied. That is, given a  3×3  magic square  A  and a  4×4  magic square  B , the product of  A  and  B , denoted by  A*B , will be a  12×12  magic square. The use of product and multiplication in this context is different than multiplication of numbers.

properties

procedure

If  A  has dimension N and  B  has dimension M then the product  C  consists of an M×M matrix of N×N sub-matrices where each element of a sub-matrix is:

 aij + N 2(bxy−1)

in which i, j are the indices of  A  and x, y are the indices of  B.  To illustrate, let

A
a11 a12 a13
a21 a22 a23
a31 a32 a33

and let

B
b11 b12 b13 b14
b21 b22 b23 b24
b31 b32 b33 b34
b41 b42 b43 b44

then we can write out

C
a11+9(b11−1) a12+9(b11−1) a13+9(b11−1) a11+9(b12−1) a12+9(b12−1) a13+9(b12−1) . . .
a21+9(b11−1) a22+9(b11−1) a23+9(b11−1) a21+9(b12−1) a22+9(b12−1) a23+9(b12−1) . . .
a31+9(b11−1) a32+9(b11−1) a33+9(b11−1) a31+9(b12−1) a32+9(b12−1) a33+9(b12−1) . . .
a11+9(b21−1) a12+9(b21−1) a13+9(b21−1) a11+9(b22−1) a12+9(b22−1) a13+9(b22−1) . . .
a21+9(b21−1) a22+9(b21−1) a23+9(b21−1) a21+9(b22−1) a22+9(b22−1) a23+9(b22−1) . . .
a31+9(b21−1) a32+9(b21−1) a33+9(b21−1) a31+9(b22−1) a32+9(b22−1) a33+9(b22−1) . . .
: : : : : : . . .
: : : : : : . . .

The calculator demonstrates multiplication and the transformation operations.

Return to Contents


| home | contents | send comment | send link | add bookmark |


Copyright © 2004, Stephen R. Schmitt