/* Last two digits (Enigma #1178) in Picat. https://enigmaticcode.wordpress.com/2016/02/29/enigma-1178-last-two-digits/ """ From New Scientist #2334, 16th March 2002 The teacher challenged the children to find a particular 5-digit number consisting of 5 different digits such that when they multiplied it by 2 they created another 5-digit number consisting of the other 5 digits. By giving the children the last two digits of the number that they had to find the teacher ensured that there was only one possible answer; but I shall be less kind and merely tell you that the last digit of the number they had to find was 1. What was that number ending in 1 that they had to find? """ 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, XA = new_list(N), XA :: 0..9, X :: 10000..99999, to_num(XA,10,X), YA = new_list(N), YA :: 0..9, Y :: 10000..99999, to_num(YA,10,Y), % last digit is 1 XA[N] #= 1, X * 2 #= Y, ZA = XA ++ YA, all_different(XA), all_different(YA), all_different(ZA), All = solve_all([],[XA,YA,ZA,X,Y]), % Put the two last digits of XA in a map LastTwo = new_map(), foreach(A in All) LT = [A[1,I] : I in 4..5], LastTwo.put(LT,LastTwo.get(LT,[])++ [A]) end, % Find the (unique) solution where there is only one occurrence % of the two digits. foreach(_=V in LastTwo) if V.length==1 then println(first=V[1,4]) % println(second=V[1,5]) end end, 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]).