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 x 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
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
Definition
A magic square is an arrangement of the numbers from 1...N2 in an NxN 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·(N2 + 1)/2
The simplest magic square is the 1x1 magic square whose only entry is the number 1. The next simplest is the 3x3 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 N2+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 3x3 magic square A and a 4x4 magic square B, the product of A and B, denoted by A*B, will be a 12x12 magic square. The use of product and multiplication in this context is different than multiplication of numbers.
properties
If A, B, and C are magic squares, then A*(B*C) = (A*B)*C.
If A is a magic square, then A*1 = A.
If A and B are magic squares, then A*B ≠ B*A.
procedure
If A has dimension N and B has dimension M then the product C consists of an MxM matrix of NxN sub-matrices where each element of a sub-matrix is:
in which i, j are the indices of A and x, y are the indices of B. To illustrate, letaij + N2·(bxy-1)
and let
A =
a11 a12 a13 a21 a22 a23 a31 a32 a33
then we can write out
B =
b11 b12 b13 b14 b21 b22 b23 b24 b31 b32 b33 b34 b41 b42 b43 b44
The calculator demonstrates multiplication and the transformation operations.
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) . . . : : : : : : . . . : : : : : : . . .
Reference
Return to Contents
AbCd Classics - free on-line books