← RoseCode

ROSECODE 419

Enumerating the magic cubes

Philippe_57721 · Programming ·

A magic cube is the 3-D equivalent of a magic square.

Here is an exemple of a 3×3×3 magic cube with all integers from 1 to 27:

- The sum of all rows and columns in the 3 directions are equals
4+26+12=42
4+18+20=42
4+17+21=42

- The sum of the 4 main diagonals are equals
4+14+24=42
21+14+7=42
16+14+12=42
20+14+8=42

How many 3×3×3 magic cubes with integers in {1,,27} are there ?

Answer format: (Count) (colon) (comma separated list of values for the cube of rank 123).

Example : 999:4,17,21,18,19,5,20,6,16,26,3,13,1,14,27,15,25,2,12,22,8,23,9,10,7,11,24
(Corresponds to the cube in the above picture)


[My timing: 2 min]
PS:
We ignore symmetries : facing the 6 faces of this example, we get 6 distinct solutions.
To rank the cubes, represent them as a 27 elements vectors (as in the above example), then sort the vectors in lexicographic order.