/* Five cards puzzle in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 10. Five cards I have five cards bearing the figures 1, 3, 5, 7, and 9. How can I arrange them in a row so that the number formed by the first pair multiplied by the number formed by the last pair, with the central number substracted, will produce a number composed of repeti- tions of one figure? Thus, in the example I have shown, 31 multiplied by 79 and 5 sub- tracted will produce 2444, which would have been all right if that 2 had happened to be another 4. Of course, there must be two solutions, for the pairs are clearly interchange- able (puzzle 103 from Dudeney 2016). """ The two (symmetric) solutions: [3,9,1,5,7] = 2222 [5,7,1,3,9] = 2222 Cf five_odd_cards.pi This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> % We don't know the size of the result member(YLen,2..10), YL = new_list(YLen), YL :: 0..9, X = new_list(5), X :: [1,3,5,7,9], all_different(X), to_num(YL,Y), all_same(YL), Y #= (10*X[1]+X[2]) * (X[4]*10+X[5]) - X[3], Vars = X ++ YL, solve(Vars), println(X=Y), fail, nl. go => true. /* If we assume that the result is a four digit number then it's a little neater model. */ go2 ?=> X = new_list(5), X :: [1,3,5,7,9], D :: 1..9, all_different(X), 1000*D+100*D+10*D+D #= (10*X[1]+X[2]) * (X[4]*10+X[5]) - X[3], solve(X ++ [D]), println(X=[D : _ in 1..4]), fail, nl. go2 => true. all_same(X) => foreach(I in 2..X.len) X[I] #= X[I-1] end. to_num(List, Base, Num) => Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]). to_num(List, Num) => to_num(List, 10, Num).