/////////////////////////////////////////////////////////////////////////////// // // A pandiagonal magic square (aka panmagic square or diabolic square) is a // magic square with the additional property that all broken diagonals, // including the diagonals that wrap round at the edges of the square, also // add up to the magic constant. // There are 3600 (28800 if rotational and reflection symmetry is allowed) // magic squares 5x5. // This program finds all 3600 5x5 magic squares that are pandiagonal. // It takes about 10 minutes using a 2.4GHz Core2 processor. // // /////////////////////////////////////////////////////////////////////////////// // // To generate all 3600 solutions, run query: // // all MagicSquares5x5Pandiagonal() // // To generate a single solution, run query: // // one MagicSquares5x5Pandiagonal() // // You may want to generate only a subset of all magic squares, for example // to generate only 100 solutions, use the following query: // // all MagicSquares5x5Pandiagonal() & RtlTrimSolutions(100) // /////////////////////////////////////////////////////////////////////////////// pred MagicSquares5x5Pandiagonal() iff ms::[0..24]->>L[1..25] & ms = [ a1, a2, a3, a4, a5, b1, b2, b3, b4, b5, c1, c2, c3, c4, c5, d1, d2, d3, d4, d5, e1, e2, e3, e4, e5 ] & // Remove symmetries a1 < a5 & a1 < e1 & a1 < e5 & a5 < e1 & // Constraints for diagonals a1 + b2 + c3 + d4 + e5 = 65 & a2 + b3 + c4 + d5 + e1 = 65 & a3 + b4 + c5 + d1 + e2 = 65 & a4 + b5 + c1 + d2 + e3 = 65 & a5 + b1 + c2 + d3 + e4 = 65 & // Constraints for inverse diagonals e1 + d2 + c3 + b4 + a5 = 65 & e2 + d3 + c4 + b5 + a1 = 65 & e3 + d4 + c5 + b1 + a2 = 65 & e4 + d5 + c1 + b2 + a3 = 65 & e5 + d1 + c2 + b3 + a4 = 65 & // Constraints for rows and colums a1 + a2 + a3 + a4 + a5 = 65 & a1 + b1 + c1 + d1 + e1 = 65 & b1 + b2 + b3 + b4 + b5 = 65 & a2 + b2 + c2 + d2 + e2 = 65 & c1 + c2 + c3 + c4 + c5 = 65 & a3 + b3 + c3 + d3 + e3 = 65 & d1 + d2 + d3 + d4 + d5 = 65 & a4 + b4 + c4 + d4 + e4 = 65 & e1 + e2 + e3 + e4 + e5 = 65 & a5 + b5 + c5 + d5 + e5 = 65 & PrettyPrint(ms,0) /////////////////////////////////////////////////////////////////////////////// local proc PrettyPrint(ms:<[0..24]->>L[1..25], row:<I) iff if row < 5 then j = row*5 & Print('\n') & PrintDigit(ms(j)) & PrintDigit(ms(j+1)) & PrintDigit(ms(j+2)) & PrintDigit(ms(j+3)) & PrintDigit(ms(j+4)) & PrettyPrint(ms,row+1) else Print('\n') end local proc PrintDigit(d:<L) iff if d < 10 then Print(' ',d,' ') else Print(d,' ') end
This page was created by F1toHTML