/* A duel in arithmetic in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 113. A duel in arithmetic The Mathematics Circle in our school had this custom: Each applicant was given a simple problem to solve a little mathematical nut to crack, so to speak. You could become a full member only if you solved the problem. An applicant named Vitia was given this array: 1 1 1 3 3 3 5 5 5 7 7 7 9 9 9 He was asked to replace 12 digits with zeros so that the sum would be 20. Vitia thought a little, then wrote rapidly two solutions: 0 1 1 + 0 1 0 + 0 0 0 + 0 0 3 + 0 0 0 + 0 0 0 + 0 0 0 + 0 0 7 + 0 0 9 0 0 0 ----- ----- 2 0 2 0 He smiled and said: "If you substitute just ten zeros for digits, the sum will be 1,111. Try it!" The Circle’s president was taken aback briefly. He not only solved Vitia’s problem, but also he improved it: "Why not replace only 9 digits with zeros and still get 1,111?" As the debate continued, ways of getting 1,111 by replacing 8, 7, 6, and 5 digits with zeros were found. Solve the six forms of this problem. (puzzle 48 from Kordemsky (1992)) """ Here are all 18 solutions after substituting 5..10 zeros give 1111. numZeros = 5 [1,1,1] [3,3,3] [5,0,0] [0,7,7] [0,9,0] y = [111,333,500,77,90] z = 1111 numZeros = 6 [1,0,0] [3,3,0] [5,0,5] [0,7,7] [0,9,9] y = [100,330,505,77,99] z = 1111 numZeros = 7 [0,1,0] [3,3,0] [0,5,5] [7,0,7] [0,0,9] y = [10,330,55,707,9] z = 1111 [0,1,1] [0,3,3] [0,0,0] [0,7,7] [9,9,0] y = [11,33,0,77,990] z = 1111 [1,1,0] [0,3,0] [0,5,5] [0,0,7] [9,0,9] y = [110,30,55,7,909] z = 1111 numZeros = 8 [0,0,0] [0,3,0] [0,0,5] [0,7,7] [9,9,9] y = [0,30,5,77,999] z = 1111 [0,0,1] [3,3,3] [0,0,0] [7,7,7] [0,0,0] y = [1,333,0,777,0] z = 1111 [0,1,1] [3,0,3] [0,0,0] [7,0,7] [0,9,0] y = [11,303,0,707,90] z = 1111 [1,0,1] [0,3,3] [0,0,0] [0,7,7] [9,0,0] y = [101,33,0,77,900] z = 1111 [1,0,1] [3,0,0] [5,5,0] [0,7,0] [0,9,0] y = [101,300,550,70,90] z = 1111 [1,1,1] [0,0,3] [0,0,0] [0,0,7] [9,9,0] y = [111,3,0,7,990] z = 1111 numZeros = 9 [0,0,0] [3,0,0] [0,0,5] [7,0,7] [0,9,9] y = [0,300,5,707,99] z = 1111 [0,1,1] [3,3,0] [0,0,0] [7,7,0] [0,0,0] y = [11,330,0,770,0] z = 1111 [1,0,0] [0,0,0] [0,0,5] [0,0,7] [9,9,9] y = [100,0,5,7,999] z = 1111 [1,0,1] [3,0,3] [0,0,0] [7,0,7] [0,0,0] y = [101,303,0,707,0] z = 1111 [1,1,1] [0,3,0] [0,0,0] [0,7,0] [9,0,0] y = [111,30,0,70,900] z = 1111 numZeros = 10 [0,0,1] [0,0,0] [0,5,0] [0,7,0] [9,9,0] y = [1,0,50,70,990] z = 1111 [1,1,1] [3,0,0] [0,0,0] [7,0,0] [0,0,0] y = [111,300,0,700,0] z = 1111 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 => N = 5, % Let's also check 1..4 and 11.12 member(NumZeros,[1,2,3,4, 5,6,7,8,9,10, 11,12]), println(numZeros=NumZeros), T = [[1,1,1], [3,3,3], [5,5,5], [7,7,7], [9,9,9]], X = new_array(N,3), X :: 0..9, Xs = X.vars, Y = new_list(N), Y :: 0..999*N, Z #= sum(Y), Z #= 1111, % Count the 0s count(0,Xs,#=,NumZeros), % Replace a 0 foreach(I in 1..N, J in 1..3) X[I,J] #> 0 #=> X[I,J] #= T[I,J] end, % Connect X and Y foreach(I in 1..N) to_num(X[I],Y[I]) end, Vars = Xs ++ Y ++ [Z], solve(Vars), foreach(Row in X) println(Row.to_list) end, println(y=Y), println(z=Z), nl, fail, nl. % % 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]). to_num(List, Num) => to_num(List, 10, Num).