/* Multiplication puzzle in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 14. Multiplication How many solutions are for: A B C D E F * 3 = B C D E F A? (puzzle from Math is fun - www.mathisfun.com) """ There are two solutions: x = [1,4,2,8,5,7] 142857 * 3 #= 428571 x = [2,8,5,7,1,4] 285714 * 3 #= 857142 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 ?=> X = [A,B,C,D,E,F], X :: 0..9, % This constraint is not stated in the description but in Groza's Mace 4 model. % Though it's not needed to getting the two solutions. % all_different(X), to_num([A,B,C,D,E,F],First), to_num([B,C,D,E,F,A],Second), A #> 0, B #> 0, First*3 #= Second, solve(X), println(x=X), println((First*3)#=(Second)), nl, fail, nl. go => true. % % converts a number Num to/from a list of integer List given a base Base % to_num(L, Base, Num) => Len = length(L), Num #= sum([L[I]*Base**(Len-I) : I in 1..Len]). to_num(L, Num) => to_num(L, 10, Num).