Reputation: 225
so i am trying to solve kenken using prolog but i ran into several problems from the start, first of all lets say i run it like kenken([X1,X2,X3,.....X16]). and i want to solve for this x's with rules i defined before. so lets say the first cell has 3 values X1,X2,and X3 and i want to get 2 by using multiplication meaning that X1*X2*X3=2, now how do i set up a rule to see all posible solutions if i had something like that. also how would i tell my x's to only use a range of values 1-4. i tried to do something like
:- use_module(library(clpr)).
solve([X1,X2,X3]):-
{X1*X2*X3=2}.
but its gives me a really weird output.
Upvotes: 2
Views: 1065
Reputation: 40768
Since you reason over integers, not floats, consider using library(clpfd)
instead of CLP(R). In SICStus, SWI and YAP, you can constrain a finite domain variable X
to the integer range 1-4 with:
X in 1..4
You can use the built-in predicate label/1
to search for concrete solutions. Example with SWI-Prolog:
?- Vars = [A,B,C], A*B*C #= 2, Vars ins 1..4, label(Vars).
yielding:
Vars = [1, 1, 2], A = B, B = 1, C = 2 ;
Vars = [1, 2, 1], A = C, C = 1, B = 2 ;
Vars = [2, 1, 1], A = 2, B = C, C = 1.
Upvotes: 3
Reputation: 49896
You need the is
operator to do arithmetic:
2 is X1*X2*X3
Note that this will not work unless the X's are all bound to numbers.
Upvotes: 0