What am I missing from this "Grocery problem (7/11 problem)"?

82 views
Skip to first unread message

meta_leap

unread,
Nov 24, 2023, 12:25:39 PM11/24/23
to MiniZinc
As I'm browsing for problems to practice on my own with (rather than copy its presented solution), I came across http://www.hakank.org/minizinc/grocery_float.mzn and decided I don't care for the float version, cents-in-ints should work if this puzzle tries to have any semblance of realism/plausibility, which I'm optimistically assuming.

Well, this one is "UNSATISFIABLE" in all solvers, what could I be missing?

% A kid goes into a grocery store and buys four items. The cashier charges $7.11.
% The kid pays and is about to leave when the cashier calls the kid back, and says
% "Hold on, I multiplied the four items instead of adding them; I'll try again...
% Gosh, with adding them the price still comes to $7.11"! What were the prices of
% the four items?
int: numItems = 4;
int: totalCents = 711;
array[1..numItems]of var 0..totalCents: itemCents;
constraint (sum(itemCents) == totalCents);
constraint (product(itemCents) == totalCents);

Jip Dekker

unread,
Nov 27, 2023, 6:16:08 PM11/27/23
to MiniZinc
This model seems correct to me (and gives correct solutions for smaller data that is easier to check).

I believe that there just is no exact solution for the problem. When solving the linked model the problem uses much more precision than just cents: "[1.2, 1.25, 1.5, 3.16000000000001]"

jack drawbridge

unread,
Nov 28, 2023, 5:19:45 PM11/28/23
to MiniZinc
This problem  using pennies is shown in section 2.7.1 of the latest Minizinc Handbook with a brief discussion.
  The solution provided:
var 1..711: item1;
var 1..711: item2;
var 1..711: item3;
var 1..711: item4;
constraint item1 + item2 + item3 + item4 == 711;
constraint item1 * item2 * item3 * item4 == 711 * 100 * 100 * 100;
constraint 0 < item1 /\ item1 <= item2 /\ item2 <= item3 /\ item3 <= item4;
solve satisfy;
And gives answer

{120,125,150,316}


My question: How would you know, or what is the logic in the constraint on product/multiplication to have *100 *100 * 100 ?

Jip Dekker

unread,
Nov 28, 2023, 5:46:21 PM11/28/23
to MiniZinc
Ah, that is a good point. Thank you for pointing it out.

I overlooked the fact that with multiplication, the order of magnitude of the chosen unit does matter. For example, 1m * 1m = 1m^2 and 100cm * 100cm = 10,000cm^2. Although they are still equivalent, they are only equivalent after converting them back to a common unit.

So since the original problem is phased in terms of dollars, when the decision variables represent cents, the constraints in dollars would be:
constraint item1/100 + item2/100 + item3/100 + item4/100 == 711/100;
constraint
 item1/100 * item2/100 * item3/100 * item4/100 == 711/100;
For the first constraints we can just multiply both sides by 100 and all divisions disappear.
For the second constraint multiplying by 100 makes the right hand side division disappear and one division on the left hand side, but there are 3 more divisions. You thus have to multiply both sides by 100 another 3 times.
You then arrive at the model in the handbook.
Reply all
Reply to author
Forward
0 new messages