/* Enigma problem 1555 (Not a square) in Picat. From New Scientist: """ Enigma Number 1555 July 25, 2009 by Richard England Not a square Fill each of the 12 cells +--+--+ | | | +--+--+--+--+ | | | | | +--+--+--+--+ | | | | | +--+--+--+--+ | | | +--+--+ with a digit so that the two four-digit numbers that you can read down, the two two-digit numbers that you can read down at the sides and the two-digit number that you can read across at the bottom are all different perfect squares (with no leading zeros). Only the two-digit number that you can read across at the top is not a perfect square. What is that two-digit number? """ The representation of the grid is A B C D E F G H I J K L Compare with this F1 Compiler model: http://www.f1compiler.com/samples/Enigma%201555.f1.html This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> All = [A,B,C,D,E,F,G,H,I,J,K,L], All :: 0..9, % leading digits A #> 0, C #> 0, G #> 0, K #> 0, B #> 0, F #> 0, % AB is not a square AB #= A*10 + B, sum([ AB #= T*T : T in 1..10]) #= 0, CDEF #= C*1000+D*100+E*10+F, square(CDEF), GHIJ #= G*1000+H*100+I*10+J, square(GHIJ), CG #= C*10+G, square(CG), FJ #= F*10+J, square(FJ), KL #= K*10+L, square(KL), ADHI #= A*1000+D*100+H*10+K, square(ADHI), BEIL #= B*1000+E*100+I*10+L, square(BEIL), all_different([CDEF,GHIJ,CG,FJ,KL,ADHI,BEIL]), Vars = All ++ [AB], solve([constr],Vars), println(all=All), println(ab=AB), nl, fail, nl. go => true. % % Ensure that X is an square % square(X) => fd_min_max(X,Min,Max), Y :: ceiling(sqrt(Min))..ceiling(sqrt(Max)), Y*Y #= X. % % converts a number Num to/from a list of integer List given a base Base % to_num(List, Base, Num) => Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]).