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
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 . . . 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
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 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 = |
|
and let
| B = |
|
then we can write out
| C = |
|
The calculator demonstrates multiplication and the transformation operations.
Return to Contents