Home More Samples
```

///////////////////////////////////////////////////////////////////////////////
//
// 1-----10-----19
// | 2     11    |20
// |   3-----12-----21
// 4   | 13     22   |
// | 5 |   14    |23 |
// |   6     15  |  24
// 7---|-16-----25   |
//   8 |   17     26 |
//     9-----18-----27
//
///////////////////////////////////////////////////////////////////////////////
//
// To generate solutions, run the query
//
//      all MagicCube3x3x3()
//
///////////////////////////////////////////////////////////////////////////////
//
// Solutions:
//
//  6 26 10       16  3 23       20 13  9
// 17  1 24       21 14  7        4 27 11
// 19 15  8        5 25 12       18  2 22
//
// ___ Solution: 1 ___ [00:00:00] __ [Backtracks: 263] ____
//
//  4 26 12       17  3 22       21 13  8
// 18  1 23       19 14  9        5 27 10
// 20 15  7        6 25 11       16  2 24
//
// ___ Solution: 2 ___ [00:00:00] __ [Backtracks: 435] ____
//
//  2 24 16       15  7 20       25 11  6
// 18  1 23       19 14  9        5 27 10
// 22 17  3        8 21 13       12  4 26
//
// ___ Solution: 3 ___ [00:00:00] __ [Backtracks: 635] ____
//
//  1 23 18       15  7 20       26 12  4
// 17  3 22       19 14  9        6 25 11
// 24 16  2        8 21 13       10  5 27
//
// ___ Solution: 4 ___ [00:00:00] __ [Backtracks: 758] ____
//
// Number of solutions: 4   Number of backtracks: 812
// Elapsed time: 00:00:00
//

local C :< L = 42

pred MagicCube3x3x3() iff

// Express the cube as an array
mc ::[0..26]->>L[1..27] &

// Decompose the array into 27 elements for easier coding
mc = [n01,n02,n03,
n04,n05,n06,
n07,n08,n09,
n10,n11,n12,
n13,n14,n15,
n16,n17,n18,
n19,n20,n21,
n22,n23,n24,
n25,n26,n27] &

// Remove symmetries...
n01 < n03 & n01 < n07 & n01 < n09 & n01 < n19 &  n01 < n21 & n01 < n25 & n01 < n27 &
n02 > n04 & n04 > n10 &

// Setup constraints between elements
n01 + n02 + n03 = C & n01 + n10 + n19 = C & n01 + n04 + n07 = C &
n04 + n05 + n06 = C & n02 + n11 + n20 = C & n02 + n05 + n08 = C &
n07 + n08 + n09 = C & n03 + n12 + n21 = C & n03 + n06 + n09 = C &
n10 + n11 + n12 = C & n04 + n13 + n22 = C & n10 + n13 + n16 = C &
n13 + n14 + n15 = C & n05 + n14 + n23 = C & n11 + n14 + n17 = C &
n16 + n17 + n18 = C & n06 + n15 + n24 = C & n12 + n15 + n18 = C &
n19 + n20 + n21 = C & n07 + n16 + n25 = C & n19 + n22 + n25 = C &
n22 + n23 + n24 = C & n08 + n17 + n26 = C & n20 + n23 + n26 = C &
n25 + n26 + n27 = C & n09 + n18 + n27 = C & n21 + n24 + n27 = C &
n01 + n14 + n27 = C & n03 + n14 + n25 = C &
n07 + n14 + n21 = C & n09 + n14 + n19 = C &

// Printout the magic cube as three 3x3 matrices
PrintMC3(mc,0)

///////////////////////////////////////////////////////////////////////////////
// n1 n2 n3    n10 n11 n12    n19 n20 n21
// n4 n5 n6    n13 n14 n15    n22 n23 n24
// n7 n8 n9    n16 n17 n18    n25 n26 n27
local proc PrintMC3(mc :< [0..26]->L, r:<I) iff
if r < 3 then
Print('\n') &
PrintDigit2(mc(3*r+0))  & PrintDigit2(mc(3*r+1))  & PrintDigit2(mc(3*r+2)) &
Print('      ') &
PrintDigit2(mc(3*r+9))  & PrintDigit2(mc(3*r+10)) & PrintDigit2(mc(3*r+11)) &
Print('      ') &
PrintDigit2(mc(3*r+18)) & PrintDigit2(mc(3*r+19)) & PrintDigit2(mc(3*r+20)) &
PrintMC3(mc,r+1)
else
Print('\n')
end

local proc PrintDigit2(d:<L) iff
if d < 10 then
Print(' ',d,' ')
else
Print(d,' ')
end

```