/* Enigma puzzle Eight times (#1317) in Picat. http://www.f1compiler.com/samples/Enigma%201317.f1.html """ Eight times Enigma 1317 Richard England, New Scientist magazine I invite you to find a 5-digit number consisting of five different digits (not starting with zero) which when multiplied by 8 produces another 5-digit number consisting of the other 5-digits; in addition the sum of the digits of your 5-digit number must be greater than the sum of the digits of the product. Which 5-digit number have you found? (Remember that the answer required is the number as it was before the multiplication.) """ 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 ?=> N = 5, X :: 10000..99999, Y :: 10000..99999, XA = new_list(N), XA :: 0..9, YA = new_list(N), YA :: 0..9, all_different(XA ++ YA), to_num(XA,10,X), to_num(YA,10,Y), Y #= 8 * X, sum(XA) #> sum(YA), Vars = XA ++ YA ++ [X,Y], solve(Vars), println(x=X), println(y=Y), nl, fail, nl. go => true. % % alternative approach % go2 ?=> Xs = [X1,X2,X3,X4,X5], Xs :: 0..9, Ys = [Y1,Y2,Y3,Y4,Y5], Ys :: 0..9, all_different(Xs ++ Ys), X #= X1*10000 + X2*1000 + X3*100 + X4*10 + X5, Y #= Y1*10000 + Y2*1000 + Y3*100 + Y4*10 + Y5, X1 #> 0, Y1 #> 0, X*8 #= Y, (X1 + X2 + X3 + X4 + X5) #> (Y1 + Y2 + Y3 + Y4 + Y5), Vars = Xs ++ Ys, solve(Vars), println(x=X), println(y=Y), nl, fail, nl. go2 => true. % % 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]).