Home More Samples
```///////////////////////////////////////////////////////////////////////////////
//
// Find all pan-triagonal and self-complementary magic cubes 4x4x4.
//
// This problem is nicely analyzed and documented by Kanji Setsuda. For more
// details see his web page:
//
//    http://homepage2.nifty.com/KanjiSetsuda/pages/epages/S123C4P1.pdf
//
// Same as Kanji Setsuda, we limit the search only to cubes with the value 1 in
// one corner and the value 64 in another corner (diagonally accross from the 1).
//
// To find 4728 solutions (n1 = 1, n64 = 64) run the query
//
//      all MagicCube4x4x4(m)
//
//
///////////////////////////////////////////////////////////////////////////////
//
// Basic Form of Magic Cube 4x4x4:
//
//      1--------17--------33--------49
//      | 2       |18       |34       |50
//      5   3    21  19    37  35    53  51
//      | 6   4--  22--20----38--36----54--52
//      9   7 |  25  23 |  41  39 |  57  55 |
//      |10   8   | 26 24 |  42  40   |58  56
//     13--11-|--29---27|--45--43-|--61  59 |
//       14  12    30   28   46  44    62  60
//         15 |      31 |      47 |      63 |
//           16--------32--------48--------64
//
///////////////////////////////////////////////////////////////////////////////

local C :< L = 130

pred MagicCube4x4x4(n::[0..63]->>L[1..64]) iff
n = [
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,n28,n29,n30,n31,n32,
n33,n34,n35,n36,n37,n38,n39,n40,
n41,n42,n43,n44,n45,n46,n47,n48,
n49,n50,n51,n52,n53,n54,n55,n56,
n57,n58,n59,n60,n61,n62,n63,n64
] &

n01 = 1 & n64 = 64 &

// n43 can have only one of the following values:
(n43 = 32 | n43 = 48 | n43 = 56 | n43 = 60 | n43 = 62 | n43 = 63) &

// Symmetry reduction
n02 > n05 &
n05 > n17 &

// Basic Conditions: C=130
n01+n02+n03+n04=C & n01+n17+n33+n49=C & n01+n05+n09+n13=C &
n05+n06+n07+n08=C & n02+n18+n34+n50=C & n02+n06+n10+n14=C &
n09+n10+n11+n12=C & n03+n19+n35+n51=C & n03+n07+n11+n15=C &
n13+n14+n15+n16=C & n04+n20+n36+n52=C & n04+n08+n12+n16=C &
n17+n18+n19+n20=C & n05+n21+n37+n53=C & n17+n21+n25+n29=C &
n21+n22+n23+n24=C & n06+n22+n38+n54=C & n18+n22+n26+n30=C &
n25+n26+n27+n28=C & n07+n23+n39+n55=C & n19+n23+n27+n31=C &
n29+n30+n31+n32=C & n08+n24+n40+n56=C & n20+n24+n28+n32=C &
n33+n34+n35+n36=C & n09+n25+n41+n57=C & n33+n37+n41+n45=C &
n37+n38+n39+n40=C & n10+n26+n42+n58=C & n34+n38+n42+n46=C &
n41+n42+n43+n44=C & n11+n27+n43+n59=C & n35+n39+n43+n47=C &
n45+n46+n47+n48=C & n12+n28+n44+n60=C & n36+n40+n44+n48=C &
n49+n50+n51+n52=C & n13+n29+n45+n61=C & n49+n53+n57+n61=C &
n53+n54+n55+n56=C & n14+n30+n46+n62=C & n50+n54+n58+n62=C &
n57+n58+n59+n60=C & n15+n31+n47+n63=C & n51+n55+n59+n63=C &
n61+n62+n63+n64=C & n16+n32+n48+n64=C & n52+n56+n60+n64=C &

//Self-Complementary Conditions:
n01+n64=65 & n02+n63=65 & n03+n62=65 & n04+n61=65 &
n05+n60=65 & n06+n59=65 & n07+n58=65 & n08+n57=65 &
n09+n56=65 & n10+n55=65 & n11+n54=65 & n12+n53=65 &
n13+n52=65 & n14+n51=65 & n15+n50=65 & n16+n49=65 &
n17+n48=65 & n18+n47=65 & n19+n46=65 & n20+n45=65 &
n21+n44=65 & n22+n43=65 & n23+n42=65 & n24+n41=65 &
n25+n40=65 & n26+n39=65 & n27+n38=65 & n28+n37=65 &
n29+n36=65 & n30+n35=65 & n31+n34=65 & n32+n33=65 &
n33+n32=65 & n34+n31=65 & n35+n30=65 & n36+n29=65 &
n37+n28=65 & n38+n27=65 & n39+n26=65 & n40+n25=65 &

//* Pan-Triagonal Conditions: C=130 *
n01+n22+n43+n64=C & n01+n24+n43+n62=C &
n01+n30+n43+n56=C & n01+n32+n43+n54=C &
n02+n23+n44+n61=C & n02+n21+n44+n63=C &
n02+n31+n44+n53=C & n02+n29+n44+n55=C &
n03+n24+n41+n62=C & n03+n22+n41+n64=C &
n03+n32+n41+n54=C & n03+n30+n41+n56=C &
n04+n21+n42+n63=C & n04+n23+n42+n61=C &
n04+n29+n42+n55=C & n04+n31+n42+n53=C &
n05+n26+n47+n52=C & n05+n28+n47+n50=C &
n05+n18+n47+n60=C & n05+n20+n47+n58=C &
n06+n27+n48+n49=C & n06+n25+n48+n51=C &
n06+n19+n48+n57=C & n06+n17+n48+n59=C &
n07+n28+n45+n50=C & n07+n26+n45+n52=C &
n07+n20+n45+n58=C & n07+n18+n45+n60=C &
n08+n25+n46+n51=C & n08+n27+n46+n49=C &
n08+n17+n46+n59=C & n08+n19+n46+n57=C &
n09+n30+n35+n56=C & n09+n32+n35+n54=C &
n09+n22+n35+n64=C & n09+n24+n35+n62=C &
n10+n31+n36+n53=C & n10+n29+n36+n55=C &
n10+n23+n36+n61=C & n10+n21+n36+n63=C &
n11+n32+n33+n54=C & n11+n30+n33+n56=C &
n11+n24+n33+n62=C & n11+n22+n33+n64=C &
n12+n29+n34+n55=C & n12+n31+n34+n53=C &
n12+n21+n34+n63=C & n12+n23+n34+n61=C &
n13+n18+n39+n60=C & n13+n20+n39+n58=C &
n13+n26+n39+n52=C & n13+n28+n39+n50=C &
n14+n19+n40+n57=C & n14+n17+n40+n59=C &
n14+n27+n40+n49=C & n14+n25+n40+n51=C &
n15+n20+n37+n58=C & n15+n18+n37+n60=C &
n15+n28+n37+n50=C & n15+n26+n37+n52=C &
n16+n17+n38+n59=C & n16+n19+n38+n57=C &
n16+n25+n38+n51=C & n16+n27+n38+n49=C
& PrettyPrint4x4x4(n,0)

////////////////////////////////////////////////////////////////////////////////
//
// Some spaghetti code to print the resulting values as the 3D following pattern:
// (Note the array index is zero-based)
//
//      1--------17--------33--------49
//      | 2       |18       |34       |50
//      5   3    21  19    37  35    53  51
//      | 6   4--  22--20----38--36----54--52
//      9   7 |  25  23 |  41  39 |  57  55 |
//      |10   8   | 26 24 |  42  40   |58  56
//     13--11-|--29---27|--45--43-|--61  59 |
//       14  12    30   28   46  44    62  60
//         15 |      31 |      47 |      63 |
//           16--------32--------48--------64
//
////////////////////////////////////////////////////////////////////////////////
local proc PrettyPrint4x4x4(n:<[0..]->L, row :< I) iff
Print('\n\t') &
if row = 0 then
Digit(n(0)) & Print('--------') & Digit(n(16)) & Print('--------') &
Digit(n(32)) & Print('--------') & Digit(n(48)) &
PrettyPrint4x4x4(n,row+1)
elsif row = 1 then
Print(' |') & Digit(n(1)) & Print('       |') & Digit(n(17)) &
Print('       |') & Digit(n(33)) & Print('       |') & Digit(n(49)) &
PrettyPrint4x4x4(n,row+1)
elsif row = 2 then
Digit(n(4)) & Print('  ') & Digit(n(2))& Print('    ') & Digit(n(20))&
Print('  ') & Digit(n(18)) & Print('    ')& Digit(n(36))&
Print('  ') & Digit(n(34)) & Print('    ') & Digit(n(52)) & Print('  ') &
Digit(n(50)) &
PrettyPrint4x4x4(n,row+1)
elsif row = 3 then
Print(' |') & Digit(n(5))& Print('  ')& Digit(n(3)) & Print('--  ') &
Digit(n(21)) & Print('--') & Digit(n(19))  & Print('----') &
Digit(n(37)) & Print('--') & Digit(n(35)) & Print('----') & Digit(n(53)) &
Print('--') & Digit(n(51)) &
PrettyPrint4x4x4(n,row+1)
elsif row = 4 then
Digit(n(8)) & Print('  ') & Digit(n(6)) & Print(' |  ') & Digit(n(24)) &
Print('  ') & Digit(n(22))& Print(' |  ') & Digit(n(40)) & Print('  ') &
Digit(n(38)) & Print(' |  ') & Digit(n(56)) & Print('  ') & Digit(n(54)) &
Print(' |') &
PrettyPrint4x4x4(n,row+1)
elsif row = 5 then
Print(' |') & Digit(n(9)) & Print('  ') & Digit(n(7)) & Print('   | ') &
Digit(n(25)) & Print(' ') & Digit(n(23)) & Print(' |  ') & Digit(n(41)) &
Print('  ') & Digit(n(39)) & Print('   |') & Digit(n(57)) & Print('  ') &
Digit(n(55)) &
PrettyPrint4x4x4(n,row+1)
elsif row = 6 then
Digit(n(12)) & Print('--') & Digit(n(10)) & Print('-|--') & Digit(n(29)) &
Print('---') & Digit(n(26))& Print('|--') & Digit(n(44))&
Print('--') & Digit(n(42)) & Print('-|--') & Digit(n(60)) & Print('  ') &
Digit(n(58)) & Print(' |') &
PrettyPrint4x4x4(n,row+1)
elsif row = 7 then
Print('  ') & Digit(n(13)) & Print('  ') & Digit(n(11)) & Print('    ')&
Digit(n(29))& Print('  ') & Digit(n(27)) & Print('    ') & Digit(n(45)) &
Print('  ') & Digit(n(43)) & Print('    ') & Digit(n(61)) & Print('  ') &
Digit(n(59)) &
PrettyPrint4x4x4(n,row+1)
elsif row = 8 then
Print('    ') &  Digit(n(14)) & Print(' |      ') & Digit(n(30)) &
Print(' |      ') & Digit(n(46)) & Print(' |      ') & Digit(n(62))&
Print(' |') &
PrettyPrint4x4x4(n,row+1)
elsif row = 9 then
Print('      ') & Digit(n(15)) & Print('--------') & Digit(n(31)) &
Print('--------') & Digit(n(47)) & Print('--------') & Digit(n(63)) &
Print('\n')
end

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

```