HomeHome More SamplesMore Samples

///////////////////////////////////////////////////////////////////////////////
// Squares from squares
///////////////////////////////////////////////////////////////////////////////
// Enigma 1553 Susan Denham, New Scientist magazine, July 11, 2009
///////////////////////////////////////////////////////////////////////////////
// 
// Your task is to place a digit in each of these 16 squares:
//
//   +--+--+--+--+
//   |  |  |  |  |  
//   +--+--+--+--+
//   |  |  |  |  |  
//   +--+--+--+--+
//   |  |  |  |  |  
//   +--+--+--+--+
//   |  |  |  |  |  
//   +--+--+--+--+
//
// When you have finished, the grid should have the following properties:
//
// * no digit occurs more than once in any row
// * the sum of the four digits in each row is the same
// * the sum of the four digits in each column is the same
// * each row should form a different four-figure perfect square.
//
// Please send in the four perfect squares in increasing order.
//
///////////////////////////////////////////////////////////////////////////////
//
// Solve the problem by running the query:
//
//       Enigma_1553()
//
///////////////////////////////////////////////////////////////////////////////
//
// Results:
//
// [1764,3249,5184,9801],Nil
// Success
// Elapsed time: 00:00:00  
//
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Collect all solutions in a list and print the list
//
proc Enigma_1553() iff
    all  x in results
        Enigma_1553x(x) 
    end & Print(results)

///////////////////////////////////////////////////////////////////////////////
//
// a b c d 
// e f g h 
// i j k l 
// m n o p
//
local pred Enigma_1553x(res :: [0..]->L) iff
    letters :: [0..]->L[0..9] &
    letters = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p]  &

    // Note there is no explicit requirement for no leading zeroes.
    // Constraints for each row...

    _AllDifferent(a,b,c,d) & sum1 = a + b + c + d & 
    _AllDifferent(e,f,g,h) & sum1 = e + f + g + h & 
    _AllDifferent(i,j,k,l) & sum1 = i + j + k + l & 
    _AllDifferent(m,n,o,p) & sum1 = m + n + o + p & 

    // There is no requirement for column digits to be different...
    sum2 = a + e + i + m & 
    sum2 = b + f + j + n & 
    sum2 = c + g + k + o & 
    sum2 = d + h + l + p & 

    // The four-figure numbers for each row...
    x1 = 1000*a + 100*b + 10*c + d & 
    x2 = 1000*e + 100*f + 10*g + h & 
    x3 = 1000*i + 100*j + 10*k + l & 
    x4 = 1000*m + 100*n + 10*o + p & 

   // We want the results in ascending order
    _AllAscending(x1,x2,x3,x4) &

    // And each four-figure number must be a perfect square...
    RtlIsPowerOf2(x1) &  
    RtlIsPowerOf2(x2) &  
    RtlIsPowerOf2(x3) &  
    RtlIsPowerOf2(x4) &

    // Return the calculated numbers in an array
    res = [x1,x2,x3,x4]
      
    
   
    




This page was created by F1toHTML