/* Enigma problem 1535 (Back to front) in Picat. From New Scientist: """ Back to front Enigma 1535 Bob Walker, New Scientist magazine, March 7, 2009. Joe has found that just a few numbers have a rather unique property. Reverse the order of the digits, and the new number is a multiple of the original. For some specified numbers of digits (four or more) there are only two numbers with the property. Joe gave Penny the task of finding the two 6-digit numbers. What is the lower 6-digit number? """ Compare to the Formula One model: http://www.f1compiler.com/samples/Enigma%201535.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 ?=> N = 6, X1 :: 100000..999999, X2 :: 100000..999999, X1A = new_list(N), X1A :: 0..9, X2A = new_list(N), X2A :: 0..9, reverse(X1A, X2A), to_num(X1A, 10, X1), to_num(X2A, 10, X2), X1 mod X2 #= 0, X1 #> X2, Vars = X1A ++ X2A ++ [X1,X2], solve([ff],Vars), println(x1=X1), println(x2=X2), println(x1a=X1A), println(x2a=X2A), nl, fail, nl. go => 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]). % % reverse an array % reverse(X, Rev) => Len = X.len, foreach(I in 1..Len) Rev[I] #= X[Len-I+1] end