/* Incredible Cube Puzzle in Picat. MindYourDecisions: """ Thanks to Kieran in New Zealand for suggesting this problem! A version of this problem was given to 14 year old students in Taiwan. A mathematician writes numbers on a cube. On each face the mathematician writes a positive integer. On each vertex the mathematician writes the product of its three adjacent faces. If the sum of all vertices is 154, what is the sum of all faces? (For clarification: a positive integer is a whole number greater than zero, such as 1, 2, 3, …) """ Video: https://youtu.be/QC1buX8hyY0 One solution (with the symmetry breaking of increasing(Faces)); faces = [1,1,1,1,6,10] vertices = [1,1,6,6,10,10,60,60] sumFaces = 20 sumVertices = 154 Without symmetry breaking, there are 360 different solutions. 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 ?=> nolog, NumFaces = 6, NumVertices = 8, SumVertices = 154, Faces = new_list(NumFaces), Faces :: 1..154, Vertices = new_list(NumVertices), Vertices :: 1..154, SumVertices #= sum(Vertices), SumFaces #= sum(Faces), % The 8 vertices (corners) Prods = [ [1,2,3], [1,2,4], [1,3,5], [1,4,5], [2,3,6], [2,4,6], [3,5,6], [4,5,6] ], foreach(I in 1..NumVertices) prod([Faces[Prods[I,J]] : J in 1..3]) #= Vertices[I] end, % symmetry breaking increasing(Faces), Vars = Faces ++ Vertices ++ [SumVertices, SumFaces], println(solve), solve($[ff,split],Vars), println(faces=Faces), println(vertices=Vertices), println(sumFaces=SumFaces), println(sumVertices=SumVertices), nl, fail, nl. go => true. % The solution given in the video is much neater. % Check the video for the details. % The main tricks are: % - given the sides of the cube is a..f (in a certain configuration). % factor out the common sides in the list of the vertices. % and set this to 154 % (a+c)*(b+d)*(e+f) #= 154 % % - realize that the sum of the sides is the sum of the factors of 154. % sum of (a+c)*(b+d)*(e+f) #= 2*7*11 % i.e. a+c = 2 % b+d = 7 % e+f = 11 % % And there are 360 solutions here as well. % go2 ?=> Total = 154, X = [A,B,C,D,E,F], X :: 1..120, (A+C)*(B+D)*(E+F) #= Total, Factors = [P : P in 2..Total div 2, prime(P), Total mod P == 0], % brute force println(factors=Factors), S #= sum(Factors), % This don't help with all the symmetries. % A+C #= Factors[1], A #<= C, % B+D #= Factors[2], B #<= D, % E+F #= Factors[3], E #<= F, solve(X), println(sum=S), % fail, nl. go2 => true.