Order 5 Magic Square Count

Counting the Squares

See C code.

Distinct Squares

Consider the numbers A, B, C, D at the corners of the center 3x3 square:

   o o o o o
   o A o B o
   o o o o o
   o C o D o
   o o o o o

The 24 permutations of these numbers can be arranged in 3 groups:

	        AB CA DC BD CD BA DB AC
	        CD DB BA AC AB DC CA BD

	        AB DA CD BC DC BA CB AD
	        DC CB BA AD AB CD DA BC

	        AC DA BD CB DB CA BC AD
	        DB BC CA AD AC BD DA CB

It is sufficient to count squares that have 1 permutation from each of these groups; the others are rotations and reflections.

Suppose that D > C > B > A. Then one strategy for counting only the distinct squares is to count only those with the first configuration in each group. That is, for the main diagonals, the second row, and the second column in the 5x5 square, count only permutations in which the fourth number is greater than the second.

Square Transformations

The number of squares to count can be reduced to 1/4 of the distinct squares by not counting the squares for these two transformations:

  1. Swap rows 1 and 5; swap columns 1 and 5.
  2. Swap rows 1 and 2, rows 4 and 5; swap columns 1 and 2, columns 4 and 5.

Consider a square X with these diagonals:

   E o o o F
   o A o B o
   o o o o o
   o C o D o
   G o o o H

Transformation 1 will change X to a square with these diagonals:

   H o o o G
   o A o B o
   o o o o o
   o C o D o
   F o o o E

Transformation 2 will change X to a square with these diagonals:

   A o o o B
   o E o F o
   o o o o o
   o G o H o
   C o o o D

One way to avoid counting these is:

Complement Pairs

Consider the number K in the center cell. It is sufficient to count squares for which K is one number of each complement pair, plus squares for which K is 13. For example, count squares with center 1 .. 12 and 13. The count of squares for center 25 is the same as the count for center 1, ..., the count for center 14 is the same as the count for center 12.

Count squares for center K from 1 .. 13. Add the count for each of centers 1 .. 12 twice in the total.

Counting - Preparation

Note: Permutations are of combinations of 5 numbers chosen from 1 .. 25 that total to the magic constant.

Make arrays:

CenterKPermus
permutations that have K as the center number, (with K omitted)
CenterKUPermus
pointers to only CenterKPermus in which the third number is greater than the second
CenterKPermus14
pointers to CenterKPermus, indexed by the first and fourth numbers
CenterKPermus23
.. indexed by the second and third numbers
OKPermus
permutations that do not contain K
OKPermus125
pointers to OKPermus, indexed by the first, second, and fifth numbers
OKUPermus24
pointers to only OKPermus in which the fourth number is greater than the second, indexed by the second and fourth numbers
OKUPermus234
.. indexed by the second, third, and fourth numbers
OKPermus1235
the fourth number in OKPermus, indexed by the first, second, third, and fifth numbers
OKPermus1245
the third number in OKPermus, indexed by the first, second, fourth, and fifth numbers

Counting - Procedure

   x
     x
       x      Get a diagonal, (CenterKUPermus loop).    
         x  
           x


   o       x
     o   x  
       o      Get the other diagonal, (CenterKUPermus loop).    
     x   o
   x       o

Check for cell[2,4] greater than cell[2,2].
Flag diagonals to avoid for above transformation 2.
Check cell[5,1] greater than cell[1,5] to disable above transformation 1.
Check if disagonals disabled.

   o       o
   x o x o x  Get row 2, (OKUPermus24 loop).
       o    
     o   o  
   o       o


   o       o
   o o o o o
   x   o    
   x o   o    Get column 1, (OKPermus125 loop).
   o       o

   o       o
   o o o o o
   o   o   x  Get column 5, (OKPermus125 loop).  
   o o   o x
   o       o

   o       o
   o o o o o
   o   o   o
   o o x o o  Get the third number of row 4, (OKPermus1245).
   o       o


   o       o
   o o o o o
   o x o x o  Get row 3, (CenterKPermus14).
   o o o o o
   o       o


   o   x   o
   o o o o o
   o o o o o  Get column 3, (CenterKPermus23).
   o o o o o
   o   x   o


   o x o   o
   o o o o o
   o o o o o  Get column 2, (OKUPermus234).
   o o o o o
   o x o   o


   o o o x o  Get the fourth number of row 1, (OKPermus1235).
   o o o o o
   o o o o o
   o o o o o
   o o o x o  Check the fourth number of row 5, (OKPermus1235).