Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Solving a puzzle

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm trying to write a prolog program (study exercise) that solves a certain type of puzzles:

We got 2 words, say: send and more and a third word, say: money. Now those words should be added to each other:
eg.

send
more
----- +
money

Each letter should be replaced with a number so that the everything checks out (each letter must be a different number). I had some ideas on how to do this, but none seems to work, could anyone give me some ideas on how to start???

Thanks a lot!
 
S E N D
M O R E
--------- +
M O N E Y

(different letter, different numbers)

splits([H|T], H, T).
splits([H|T], X, [H|TX]) :-
splits(T, X, TX).

oplossing([S,E,N,D,M,O,R,E,M,O,N,E,Y]) :-
splits([0,1,2,3,4,5,6,7,8,9], D, Ds),
splits(Ds, E, Es),
Y is (D+E) mod 10,
C1 is (D+E) div 10,
splits(Es, Y, Ys),
splits(Ys, N, Ns),
R is (E-C1-N+20) mod 10,
C2 is (N+R+C1) div 10,
splits(Ns, R, Rs),
O is (N-C2-E+20) mod 10,
C3 is (E+O+C2) div 10,
splits(Rs, O, Os),
splits(Os, S, Ss),
M is (O-C3-S+20) mod 10,
M is (S+M+C3) div 10,
splits(Ss, M, _).

this is a solution to this problem... as explained on many manuals and prolog references..

the idea is that you solve this problem with backtracking.
you give a char a number and continue. if for some reason it is a number you cant use it will start backtracking and tries a different number
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top