samedi 31 mai 2014

How to get a list of the Value of a number as the sum of 4 numbers in Prolog


Vote count:

0




I'm having trouble making this predicate work. The idea is to use diabolic([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]) to obtain all the possible magic squares in that list.


At first I thought about using permutation/2 but it's hella slow for a list of 16 numbers.


Then I found an example here which uses an external library (clpfd) and has awesome performance, but I'm trying to solve it without any external library... so I tried something like this:



sum([X,Y,Z,W]) :-
A = [1..16],
member(X,A),
member(Y,A),
member(Z,A),
member(W,A),
X \== Y,
X \== Z,
X \== W,
Y \== Z,
Y \== W,
Z \== W,
34 is (X+Y+Z+W).


What I'm trying to do there is getting all the possible lists of different numbers which sum is 34 so I can then check which combination makes a magic square (in hopes of making it faster that using normal permutation.


Still, I'm getting an error about some Operator Expected in member(X,[1..16]), so maybe i'm doing something wrong. I'm pretty new to Prolog so I was hoping to get some help from you guys.


Thanks in advance.



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire