Re: Riddle Solver send+more=money



Torkel Franzen a écrit :
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).




Some times ago, I've found a very interesting page (I archived it obecause I'm fed up to loose all the cool pages every year) about the magic of DCG, you'll find some cool way to solve the SEND money problem..


http://djame.seddah.free.fr/papiers/DCG_magic.html


have fun.....


Djamé

.



Relevant Pages

  • Re: Riddle Solver send+more=money
    ... Andreas Kochenburger writes: ... all I get is error messages. ... > an elegant solution, not a brute force looping, that would be faster ...
    (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)