Table of Contents

Strategies in counting magic squares

For the definitions of terms, see this.

Preparing 2N+2 magic series

To construct a magic square of order N, We need 2N+2 magic series for rows, columns and diagonals which satisfy the following constraints:

We generate sets of rows and of columns as sorted sets reserving freedom of shuffling the members of each set. And, at this stage, we allow the elements of diagonal magic series scattered in a square, not in a diagonal line, so we call them 'candidates'.

Excluding duplicates

By definition and for efficiency, we have to avoid duplicates from rotations and reflections. These transformations of order 8 can be expressed by the following three generating transformations:

  1. reflection with respect to the down diagonal line (, rows-columns exchange, or transposition)
  2. horizontal reflection
  3. 180deg rotation (or horizontal/vertical simultaneous reflection)

Duplicates from the above 1. and 2. can be eliminated by adopting the following technical constraints, respectively.

Transformation 3. is a part of M-transformations and will be discussed in the last subsection.

Complementary pair

(Inaccurate description was corrected on 2024.06.03. The codes had been correct.)

Most of magic squares have their complementary counterparts and we can save counting efforts by producing only one of a pair. We, however, have to be careful in handling exceptional cases in which the complement of a magic square happens to be equivalent to the original one.

We introduce three cases in which the largest row magic series is

  1. greater than,
  2. less than. or
  3. equal to

the largest complement of row/column magic series.

We can omit counting the case 2 by doubling the count for the case 1 because the cases 1 and 2 transform into each other under the complementary transformation, In the case 3, we just count magic squares without doubling.

Note that all squares in the case 3. are not necessarily self-complementary. You may break down the case 3. into finer cases to reduce redundancy further if you can handle them with low overhead.

An efficient method to find magic squares

Given 2N+2 magic series satisfying the above conditions, we have not yet known whether the diagonal candidate magic series can be aligned correctly. We have to check if magic squares can be constructed using the freedom reserved in the first subsection.

in mathematical terms