/* Enigma 248 Add or Multiply in Picat. https://enigmaticcode.wordpress.com/2015/01/05/enigma-248-add-or-multiply/ """ From New Scientist #1395, 2nd February 1984 I gave each of the five boys a different amount of money, and said to each: "Spend all this on goodies at the corner shop. Each goodie you buy must cost a different exact number of half-pennies. When you multiply together the prices (in pence) of all your purchases, the result must be the same as if you added them." When they came back, they had all done as I asked: each had bought the same number of goodies; and in one case each of the prices was an exact whole number of pence. How much did my generosity cost me altogether? """ This is the unique solution: total: [6.0, 7.5, 9.0, 10.5, 15.0] number of half-pennies: [2, 4, 6] [2, 3, 10] [1, 8, 9] [1, 6, 14] [1, 5, 24] pennies: [1.0, 2.0, 3.0] total: 6.0 [1.0, 1.5, 5.0] total: 7.5 [0.5, 4.0, 4.5] total: 9.0 [0.5, 3.0, 7.0] total: 10.5 [0.5, 2.5, 12.0] total: 15.0 sum_total: 48.0 This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. % % pure logic programming % go => L = [I*5/10 : I in 1..30], N = 5, % number of people % number of products (to find out) between(2,20,M), println(m=M), T = new_array(N,M), Sums = new_list(N), foreach(J in 1..M) member(T[1,J], L), % "in one case each of the prices was an exact whole number of pence." T[1,J] = ceiling(T[1,J])*1.0 end, foreach(J in 2..M) T[1,J-1] < T[1,J] end, Sums[1] = sum([T[1,J] : J in 1..M]), Sums[1] = prod([T[1,J] : J in 1..M]), foreach(I in 2..N) foreach(J in 1..M) member(T[I,J], L) end, foreach(J in 2..M) T[I,J-1] < T[I,J] end, Sums[I] = sum([T[I,J] : J in 1..M]), Sums[I] = prod([T[I,J] : J in 1..M]) end, foreach(I in 2..N) T[1] != T[I] end, foreach(I in 1..N, J in I+1..N) Sums[I] != Sums[J] end, foreach(I in 3..N) Sums[I-1] > Sums[I] end, foreach(I in 1..N) println(T[I]) end, println(sums=Sums), println(total=sum(Sums)), nl.