Re: Riddle Solver send+more=money



Andreas Kochenburger <akk@xxxxxxxxxx> writes:

> I fumbled and fumbled... all I get is error messages. I am looking for
> an elegant solution, not a brute force looping, that would be faster
> in C.

What's wrong with brute force? A second here or there is of no
consequence.

A Google search for send more money prolog yields many solutions.
Here is one, from
http://clip.dia.fi.upm.es/~vocal/public_info/seminar_notes/node13.html.

smm :-
X = [S,E,N,D,M,O,R,Y],
Digits = [0,1,2,3,4,5,6,7,8,9],
assign_digits(X, Digits),
M > 0,
S > 0,
1000*S + 100*E + 10*N + D +
1000*M + 100*O + 10*R + E =:=
10000*M + 1000*O + 100*N + 10*E + Y,
write(X).

select(X, [X|R], R).
select(X, [Y|Xs], [Y|Ys]):- select(X, Xs, Ys).

assign_digits([], _List).
assign_digits([D|Ds], List):-
select(D, List, NewList),
assign_digits(Ds, NewList).

.



Relevant Pages

  • Re: Riddle Solver send+more=money
    ... an elegant solution, not a brute force looping, that would be faster in C. ... A Google search for send more money prolog yields many solutions. ...
    (comp.lang.prolog)
  • Re: Changing date field in a text file...
    ... Chris F.A. Johnson wrote: ... > There are times when a brute force approach makes more sense than ... > an elegant solution: ...
    (comp.unix.shell)