Grogono Photo Pages
Size:  Index   3x3   4x4   5x5   6x6   7x7   8x8   9x9   10x10   11x11   12x12   13x13 

The Formula for Magic Squares

Why this "Formula" Page.

One of the commonest questions I receive by e-mail is: "What is the formula for Magic Squares?" Rather than go on answering each e-mail individually, I thought I would write this page. So, as promised in the introduction page, here is "The Formula".

A Single Formula?

Unfortunately, No!. It would be nice to find that a single simple equation would generate magic squares of any order. With the diversity of patterns used to make the different squares, it is obvious that no single equation could exist. In addition, some of the equations must be fairly complex. There is, however, a simple formula for the prime number regular pan-magic squares, e.g., for orders 5, 7, 11, 13, etc.

Knight's Move Basis.

The following formula is based on the Knight's Move technique for making magic squares. It is broken into two components, one for generating the low order bit pattern and one for the high order bit pattern.

MOD(x - 3y, 5)
MOD(x - 2y, 5)

An example of the two patterns.

The order 5 square is the smallest odd number pan-magic square which can be made using the simple Knight's Move formula. To understand how the equation is created, look at the two sets of bit patterns to the right with their corresponding equations. In each square imagine that the columns (x) are numbered 0 to 4 across the top and the rows (y) are numbered 0 to 4 down down the side. The equation shows how each cell can be calculated.


The "MOD" in the equations used here means "Modulus".
MOD(m, n) equals the "remainder" when the m is divided by n.
For example, each of the following equations equals 2:
      MOD(12, 5); MOD(7, 5); MOD(2, 5); MOD(-3, 5);


The Pan-Magic Square

The first of the above two squares is multiplied by 5 and then added to the second square to produce the regular pan-magic square on the left. This corresponds to the following equation:

z = 5 ( MOD ( x - 2 y , 5 ) ) + MOD ( x - 3 y , 5 )

It is arbitrary which square is to be the "high-order" square and multiplied by five. Either choice produces a pan-magic square.

Possible Knight's Moves
Order Step Length
5 2x1, 3x1
7 2x1, 3x1, 4x1, 5x1
11 2x1, 3x1, 4x1, 5x1,
6x1, 7x1, 8x1, 9x1
13 2x1, 3x1, 4x1, 5x1, 6x1,
7x1, 8x1, 9x1, 10x1, 11x1

Classic Knight's Move.

The above formula assumes a classical "Knight", jumping two steps forward and one step to the side (2x1) and an "extended" knight jumping three steps forward and one step to the side (3x1), which is logically equivalent to jumping 2x1 in the other axis. With larger squares provision must be made for even longer jumps. This table shows the Order of the square and the lengths of steps available up to order 13.

Number Possible.

In general, for a prime number square of order N, there are N-3 possible Knight's moves which range in length from 2 to N-2

The General Formula:

The above equation can be generalized for any size of prime number square.

Where N is the Order of the magic square, and A and B are the "lengths" of the "Knight's move" such that A and B are not equal and fall in the range between 2 and N-2.


This formula works well with prime number squares, e.g., orders 5, 7, 11, 13, 17, 19, 23, etc. The formula also appears to work with squares which are the products of primes (larger than 3x3) such as Order 25.


I have yet to check higher orders whose orders are multiples of prime numbers, e.g., 35 and 49, but on the next long flight I will try and remember to check.

Horiz Bar

Even Number Squares:

The squares whose orders are multiples of four can also be expressed as equations, but they are more complex and, therefore, less intuitive and less satisfying. For example one equation for each cell of an order 4 pan-magic square is:

z = 8 * MOD ( INT ( x/2 + y ), 2)            
+ 4 * MOD ( INT ( y/2 + x ), 2 )        
+ 2 * MOD ( INT ( x/2 +y - 0.5 ), 2 )
+ 1 * MOD ( INT ( y/2 +x - 0.5 ), 2 )


Copyright © Mar 2010 Magic Squares
Mar 6, 2010