solve(Amounts) :-
Total = 1505,
Prices = [215, 275, 335, 355, 420, 580],
length(Prices, N),
length(Amounts, N),
Amounts :: 0..Total//min(Prices),
Amounts * Prices #= Total,
labeling(Amounts).
Asked
Active
Viewed 173 times
2
-
2That's not SWI-Prolog. Presumably it's [tag:eclipse-prolog] – false Mar 02 '16 at 10:40
-
Why do you say there's something wrong with it? – lurker Mar 06 '16 at 11:21
2 Answers
5
There is nothing wrong with it. It is the example from http://eclipseclp.org/examples/xkcd287.ecl.txt, and if you hadn't omitted the line
:- lib(ic).
which loads the interval constraint solver, it would work just fine in ECLiPSe Prolog.

jschimpf
- 4,904
- 11
- 24
-
2(cf. http://xkcd.com/287/) --- ... and `lib(ic)` is what, library for integer constraints? – Will Ness Mar 07 '16 at 20:18
-
2`lib(ic)` stands for interval constraints. It implements constraints over integers and reals (represented as floating point intervals). I'll amend my answer. – jschimpf Mar 08 '16 at 21:42
2
Does also work in SWI-Prolog:
?- use_module(library(clpfd)).
?- [user].
solve(Amounts) :-
Total = 1505,
Prices = [215,275,335,355,420,580],
length(Prices, N),
length(Amounts, N),
min_list(Prices, MinPrice),
MaxAmount is Total//MinPrice,
Amounts ins 0..MaxAmount,
scalar_product(Prices, Amounts, #=, Total),
label(Amounts).
^D
?- solve(X).
X = [1, 0, 0, 2, 0, 1] ;
X = [7, 0, 0, 0, 0, 0].
But I guess its not an optimization search problem,
the objective function is missing.
Bye