HomeHome More SamplesMore Samples

///////////////////////////////////////////////////////////////////////////////
// Portuguese squares
///////////////////////////////////////////////////////////////////////////////
// Enigma 1476 Richard England, New Scientist magazine, January 12, 2008
///////////////////////////////////////////////////////////////////////////////
//
// Zero, um, nove and cme are Portuguese for 0,1,9, and 100; so it is 
// appropriate that I can make the folowing statement: 
//      ZERO,UM,NOVE and CEM are perfect squares.
// In this statement digits have been consistently replaced by capital letters,
// different letters being used for different digits. No number starts with a 
// zero.
//
// Calculate the numerical value of square root of 
//      (ZERO*UM*NOVE*CEM)
// 
///////////////////////////////////////////////////////////////////////////////
//
// Solve the problem by running the query:
//
//          all Portuguese_squares(square_root)
//
///////////////////////////////////////////////////////////////////////////////
//
// Results:
//
// square_root = 1240092
// ___ Solution: 1 __________________________________
// 
// Number of solutions: 1   Number of backtracks: 58860
// Elapsed time: 00:00:01   
//
///////////////////////////////////////////////////////////////////////////////
//
// Notes: 
// Since the relations between various numbers are non-linear, the 
// program will eventually revert to backtracking over possible values. 
// Backtracking over a variable values needs a range of possible values: 
// for example if ZERO = x1*x1, then ZERO can be at most 9876 and 
// therefore it is safe to assume x <= 1000, as 1000*1000 is certainly more
// than 9999. Obviously, we could have come with much tighter range estimates,
// but there was no real compelling reason to do so.
// 
///////////////////////////////////////////////////////////////////////////////

pred Portuguese_Squares(sr::[1..]) iff
    digits::[0..8]->>L[0..9] & digits = [z,e,r,o,u,m,n,v,c] &
    x1::[1..1000] & x1*x1 = z*1000+e*100+r*10+o & z <> 0 &   
    x2::[1..10]   & x2*x2 =              u*10+m & u <> 0 &
    x3::[1..1000] & x3*x3 = n*1000+o*100+v*10+e & n <> 0 &
    x4::[1..100]  & x4*x4 =        c*100+e*10+m & c <> 0 &
    sr = x1*x2*x3*x4                                                        






This page was created by F1toHTML