/*
Mango puzzle in Picat.
From Martin Chlond Integer Programming Puzzles:
http://www.chlond.demon.co.uk/puzzles/puzzles1.html, puzzle nr. 9.
Description : Mango puzzle
Source : M Kraitchik - Mathematical Recreations (P32)
"""
Three men who had a monkey bought a pile of mangoes. At night one of the men
came to the pile of mangoes while the others slept and, finding that there was
just one more mango than could be exactly divided by three, tossed the extra
mango to the monkey and took away one third of the remainder. Then he went
back to sleep.
Presently another of them awoke and went to the pile of mangoes. He also found
just one too many to be divided by three so he tossed the extra one to the monkey,
took one third of the remainder, and returned to sleep.
After a while the third rose also, and he too gave one mango to the monkey
and took away the whole number of mangoes which represented precisely one
third of the rest.
Next morning the men got up and went to the pile. Again they found just one
too many, so they gave one to the monkey and divided the rest evenly. What is
the least number of mangoes with which this can be done?
(Kraitchik)
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
% import util.
import cp.
main => go.
go =>
N = 5,
% decision variables
X = new_list(N),
foreach(I in 1..N)
X[I] #>= 0
end,
-3*X[1]+X[2] #= 1,
-3*X[2]+2*X[3] #= 2,
-3*X[3]+2*X[4] #= 2, % multiplies by 2 for integer version
-3*X[4]+2*X[5] #= 2, % ibid
solve($[min(X[N])],X),
println(X),
nl.
go2 =>
N = 5,
% decision variables
X = new_list(N),
foreach(I in 1..N)
X[I] #>= 0
end,
-3*X[1]+X[2] #= 1,
foreach(I in 2..N-1)
-3*X[I]+2*X[I+1] #= 2
end,
solve($[min(X[N])],X),
println(X),
nl.