We have the following constraint:
S E N D
+ M O R E
M O N E Y
the value assigned to each alphabets differs from each other.
-Solution:
This code is written in visual prolog and the output is at the end of this code.
int_list=integer*
PREDICATES
solution(int_list)
member(integer,int_list)
CLAUSES
solution ([]).
solution ([S,E,N,D,M,O,R,Y]):-
C4=1,
member(C1,[0,1]),
member(C2,[0,1]),
member(C3,[0,1]),
member(S,[0,1,2,3,4,5,6,7,8,9]),
member(E,[0,1,2,3,4,5,6,7,8,9]),
member(N,[0,1,2,3,4,5,6,7,8,9]),
member(D,[0,1,2,3,4,5,6,7,8,9]),
member(M,[0,1,2,3,4,5,6,7,8,9]),
member(O,[0,1,2,3,4,5,6,7,8,9]),
member(R,[0,1,2,3,4,5,6,7,8,9]),
member(Y,[0,1,2,3,4,5,6,7,8,9]),
S<>E,S<>N,S<>D,S<>M,S<>O,S<>R,S<>Y,
E<>N,E<>D,E<>M,E<>O,E<>R,E<>Y,
N<>D,N<>M,N<>O,N<>R,N<>Y,
D<>M,D<>O,D<>R,D<>Y,
M<>O,M<>R,M<>Y,
O<>R,O<>Y,
R<>Y,
D+E=Y+10*C1,
N+R+C1=E+10*C2,
E+O+C1=N+10*C3,
S+M+C3=O+10*C4,
M=C4.
member(X,[X|_]).
member(X,[_|Z]):-member(X,Z).
GOAL
solution([S,E,N,D,M,O,R,Y]).
%OUTPUT:
%S=9, E=5, N=6, D=7, M=1, O=0, R=8, Y=2
%S=9, E=6, N=7, D=6, M=1, O=0, R=8, Y=2
%2 Solutions
Comments
Post a Comment