Search code examples
listprologmember

Prolog member predicate possibilities rule


A premium pizza is comprised of exactly 40 ounces of toppings. There can be multiple entries of each topping, as long as the total weight is equal to 40 ounces.

Pepperoni           4 
Sausage             10
Bacon               6
Onion               5
Mushroom            7

For example, a pizza can contain 1 topping of pepperoni, 2 toppings of sausage, 1 topping of bacon, and 2 toppings of onion : 1*4 + 2*10 + 1*6 + 2*5 = 40 (ounces)

A pizza cannot contain 7 toppings of bacon : 7 * 6 = 42 > 40A pizza cannot contain only 3 toppings of sausage : 3*10 = 30 < 40

Define a rule pizza(P, S, B, O, M) to find out how many of each topping can be contained on a pizza, where P, S, B, O, and M are the weight (in ounces) of the Pepperoni, Sausage, Bacon, Onion, and Mushroom toppings respectively.

pizza(P,S,B,O,M):-
member(P,[0,1,2,3,4,5,6,7,8,9,10]),
member(S,[0,1,2,3,4]),
member(B,[0,1,2,3,4,5,6]),
member(O,[0,1,2,3,4,5,6,7,8]),
member(M,[0,1,2,3,4,5]),
S is 4*P + 10*S + 6*B + 5*O + 7*M,
S =:= 40.

I have defined the following rule but it returns false instead of the possible combination of toppings.


Solution

  • You are usingS for both the sausage weight and the weight of the whole pizza.