See C code.

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

o o o o o oAoBo o o o o o oCoDo 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.

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:

- Swap rows 1 and 5; swap columns 1 and 5.
- 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:

Eo o oFoAoBo o ooo o oCoDoGo o oH

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

Ho o oGoAoBo o ooo o oCoDoFo o oE

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

Ao o oBoEoFo o ooo o oGoHoCo o oD

One way to avoid counting these is:

- transformation 2: disable the diagonal pair
(
**AEoHD**,**BFoGC**). - transformation 1: count only squares having the bottom left corner number greater than the top right corner number

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.

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

xxxGet a diagonal, (CenterKUPermus loop).xxoxoxo Get the other diagonal, (CenterKUPermus loop).xoxo 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 oxoxoxGet row 2, (OKUPermus24 loop). o o o o o o o o o o o oxoxo o Get column 1, (OKPermus125 loop). o o o o o o o o o o oxGet column 5, (OKPermus125 loop). o o oxo o o o o o o o o o o o o oxo o Get the third number of row 4, (OKPermus1245). o o o o o o o o o oxoxo Get row 3, (CenterKPermus14). o o o o o o o oxo o o o o o o o o o o Get column 3, (CenterKPermus23). o o o o o oxo oxo o o o o o o o o o o o Get column 2, (OKUPermus234). o o o o o oxo o o o oxo 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 oxo Check the fourth number of row 5, (OKPermus1235).