/* Twenty survivors in Picat. From Chris Smith MathNewsletter #495 """ Twenty survivors of a shipwreck end up on a desert island. The leader of the group decides that the twenty food rations they have should be divided using his system: - each woman will be given three rations, - each man will be given two rations, - each child will be given half a ration. How many men, women and children have been shipwrecked? """ 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. % % Cp approach % go ?=> N = 20, F :: 0..20 div 3, % female M :: 1..20 div 2, % male, at least one mal ("his system") C :: 0..20*2, % children C mod 2 #= 0, % There must be an integer number of children. N #= 3*F + 2*M + C div 2, Total #= F + M + C, Total #= 20, solve([F,M,C,Total]) , println([females=F,men=M,children=C,total=Total]), fail, nl. go => true. % % Using a loop % go2 ?=> N = 20, foreach(F in 0..20, M in 1..20, C in 0..20) if N == 3*F + 2*M + (C div 2), N== F+M+C, C mod 2 == 0 then println([f=F,m=M,c=C, test=(3*F + 2*M + (C // 2))] ) end end, nl. go2 => true.