60-------- 1--------56--------13
         | 6       |63       |10       |51
         7  43    62  18    11  39    50  30
         |57  21--   4--48----53--25----16--36
        26  24 |  35  45 |  22  28 |  47  33 |
         |40  42   | 29 19 |  44  38   |17  31
        37-- 9-|--32---52|--41-- 5-|--20  64 |
          27  55    34  14    23  59    46   2
            54 |      15 |      58 |       3 |
              12--------49-------- 8--------61

m = [60,6,43,21,7,57,24,42,26,40,9,55,37,27,54,12,1,63,18,48,62,4,45,19,35,29,52,14,32,34,15,49,56,10,39,25,11,53,28,38,22,44,5,59,4
1,23,58,8,13,51,30,36,50,16,33,31,47,17,64,2,20,46,3,61]
___ Solution: 960 __________________________________

Number of solutions: 960   Number of backtracks: 4064410
Elapsed time: 00:04:39    

  
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
        ] &




// Symmetry reduction

n32 < n15 &
n15 < n12 &
    

n16 < n49 &
n16 < n04 &
n16 < n52 &
n16 < n01 &
n16 < n13 &
n16 < n64 &
n16 < n61 &


 // 4 major diagonals across the cube

n01 + n22 + n43 + n64 = C&
n49 + n38 + n27 + n16 = C&
n13 + n26 + n39 + n52 = C&
n04 + n23 + n42 + n61 = C&

// second half of most-perfect critera

n01 + n43 = 65 &
n22 + n64 = 65 &

n16 + n38 = 65 &
n27 + n49 = 65 &

n13 + n39 = 65 &
n26 + n52 = 65 &

n04 + n42 = 65 &
n23 + n61 = 65 &














  // 108 2x2 planar subsets sum to 130  most-perfect conditions

   // 2x2 cell blocks on the x,y plane

   n01 + n17 + n02 + n18 = C &
   n17 + n33 + n18 + n34 = C &
   n33 + n49 + n34 + n50 = C &

   n02 + n18 + n03 + n19 = C &
   n18 + n19 + n34 + n35 = C &
   n34 + n35 + n50 + n51 = C &
   
   n03 + n04 + n19 + n20 = C &
   n19 + n20 + n35 + n36 = C &
   n35 + n36 + n51 + n52 = C &

   n05 + n06 + n21 + n22 = C &
   n21 + n22 + n37 + n38 = C &
   n37 + n38 + n53 + n54 = C &

   n06 + n07 + n22 + n23 = C &
   n22 + n23 + n38 + n39 = C &
   n38 + n39 + n54 + n55 = C &

   n07 + n08 + n23 + n24 = C &
   n23 + n24 + n39 + n40 = C &
   n39 + n40 + n55 + n56 = C &

   n09 + n10 + n25 + n26 = C &
   n25 + n26 + n41 + n42 = C &
   n41 + n42 + n57 + n58 = C &

   n10 + n11 + n26 + n27 = C &
   n26 + n27 + n42 + n43 = C &
   n42 + n43 + n58 + n59 = C &

   n11 + n12 + n27 + n28 = C &
   n27 + n28 + n43 + n44 = C &
   n43 + n44 + n59 + n60 = C & 

   n13 + n14 + n29 + n30 = C &
   n29 + n30 + n45 + n46 = C &
   n45 + n46 + n61 + n62 = C &

   n14 + n15 + n30 + n31 = C &
   n30 + n31 + n46 + n47 = C &
   n46 + n47 + n62 + n63 = C &

   n15 + n16 + n31 + n32 = C &
   n31 + n32 + n47 + n48 = C &
   n47 + n48 + n63 + n64 = C &

// 2x2 cell blocks on the x,z plane

   n01 + n02 + n05 + n06 = C &
   n17 + n18 + n21 + n22 = C &
   n33 + n34 + n37 + n38 = C &
   n49 + n50 + n53 + n54 = C &

   n02 + n03 + n06 + n07 = C &
   n18 + n19 + n22 + n23 = C &
   n34 + n35 + n38 + n39 = C &
   n50 + n51 + n54 + n55 = C &

   n03 + n04 + n07 + n08 = C &
   n19 + n20 + n23 + n24 = C &
   n35 + n36 + n39 + n40 = C &
   n51 + n52 + n55 + n56 = C &

   
   n05 + n06 + n09 + n10 = C &
   n21 + n22 + n25 + n26 = C &
   n37 + n38 + n41 + n42 = C &
   n53 + n54 + n57 + n58 = C &

   n06 + n07 + n10 + n11 = C &
   n22 + n23 + n26 + n27 = C &
   n38 + n39 + n42 + n43 = C &
   n54 + n55 + n58 + n59 = C &

   n07 + n08 + n11 + n12 = C &
   n23 + n24 + n27 + n28 = C &
   n39 + n40 + n43 + n44 = C &
   n55 + n56 + n59 + n60 = C &
    

   n09 + n10 + n13 + n14 = C &
   n25 + n26 + n29 + n30 = C &
   n41 + n42 + n45 + n46 = C &
   n57 + n58 + n61 + n62 = C &

   n10 + n11 + n14 + n15 = C &
   n26 + n27 + n30 + n31 = C &
   n42 + n43 + n46 + n47 = C &
   n58 + n59 + n62 + n63 = C &

   n11 + n12 + n15 + n16 = C &
   n27 + n28 + n31 + n32 = C &
   n43 + n44 + n47 + n48 = C &
   n59 + n60 + n63 + n64 = C &

   

// 2x2 cell blocks on the y,z plane

   n01 + n17 + n05 + n21 = C &
   n02 + n18 + n06 + n22 = C &
   n03 + n19 + n07 + n23 = C &
   n04 + n20 + n08 + n24 = C &

   n17 + n33 + n21 + n37 = C &
   n18 + n34 + n22 + n38 = C &
   n19 + n35 + n23 + n39 = C &
   n20 + n36 + n24 + n40 = C &

   n33 + n49 + n37 + n53 = C &
   n34 + n50 + n38 + n54 = C &
   n35 + n51 + n39 + n55 = C &
   n36 + n52 + n40 + n56 = C &


   n05 + n21 + n09 + n25 = C &
   n06 + n22 + n10 + n26 = C &
   n07 + n23 + n11 + n27 = C &
   n08 + n24 + n12 + n28 = C &

   n21 + n37 + n25 + n41 = C &
   n22 + n38 + n26 + n42 = C &
   n23 + n39 + n27 + n43 = C &
   n24 + n40 + n28 + n44 = C &

   n37 + n53 + n41 + n57 = C &
   n38 + n54 + n42 + n58 = C &
   n39 + n55 + n43 + n59 = C &
   n40 + n56 + n44 + n60 = C &

   

   n09 + n25 + n13 + n29 = C &
   n10 + n26 + n14 + n30 = C &
   n11 + n27 + n15 + n31 = C &
   n12 + n28 + n16 + n32 = C &

   n25 + n41 + n29 + n45 = C &
   n26 + n42 + n30 + n46 = C &
   n27 + n43 + n31 + n47 = C &
   n28 + n44 + n32 + n48 = C &

   n41 + n57 + n45 + n61 = C &
   n42 + n58 + n46 + n62 = C &
   n43 + n59 + n47 + n63 = C &
   n44 + n60 + n48 + n64 = 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(28)) & 
        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