/* Kordemsky's Palindrome problem in Picat. From http://www.mathteacherctk.com/blog/2014/01/kordemskys-palindrome-problem/ """ Find a 10-digit number, with all digits distinct, whose quotient of division by 9 is a palindrome, i.e., a number that is read the same from both ends. """ There are 626 solutions, e.g. x = 1206453879 y = 134050431 x = 1206543879 y = 134060431 .. x = 8706453921 y = 967383769 x = 8706543921 y = 967393769 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 = 10, M = 9, % decision variables XA = new_list(N), XA :: 0..9, % 1234567890 1234567890 X :: 1000000000..9999999999, YA = new_list(M), YA :: 0..9, % 123456789 123456789 Y :: 10000000..999999999, to_num(XA,10,X), all_different(XA), to_num(YA,10,Y), Y #= X div 9, % y is a palindrome foreach(I in 1..M div 2) YA[I] = YA[M-I+1] end, Vars = XA ++ YA ++ [X,Y], solve(Vars), println(x=X), println(y=Y), 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]).