fork(2) download
  1. % fadder(A,B,C,D,E) :- A + B + C = 10E + D
  2. :-dynamic(fadder/5).
  3. fadder0(A,B,C,D,E) :- between(0,9,A),between(0,9,B),between(0,1,C),
  4. D is (A + B + C) mod 10, E is floor((A +B + C)/ 10), asserta(fadder(A,B,C,D,E)).
  5. :- setof(fadder(A,B,C,D,E),fadder0(A,B,C,D,E),L), member(X,L),asserta(X),fail;true.
  6. :- compile_predicates([fadder/5]).
  7. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  8. fukumen(A,B,C) :-
  9. setof(V,varof([A,B,C],V),Vars),
  10. setof([A,B,C],(fukumen0(A,B,C,0),alldifferent(Vars)),L),member([A,B,C],L).
  11.  
  12. fukumen0([A|X],[B|Y],[D|Z],E) :- fadder(A,B,C,D,E), D \= 0, fukumen1(X,Y,Z,C).
  13. fukumen1([],[],[],0) :- !.
  14. fukumen1([A|X],[B|Y],[D|Z],E) :- fadder(A,B,C,D,E), fukumen1(X,Y,Z,C).
  15.  
  16. varof(T,T) :- var(T),!.
  17. varof(T,_) :- ground(T),!,fail.
  18. varof(T,V) :- arg(_,T,A),varof(A,V).
  19.  
  20. alldifferent(X) :- nth0(I,X,A),nth0(J,X,B),A==B, I \= J,!,fail.
  21. alldifferent(_).
  22. %% ----------------------------------------------
  23. :- write('SEND + MORE = MONEY'), nl,fukumen([0,S,E,N,D],[0,M,O,R,E],[M,O,N,E,Y]) ,
  24. write([S,E,N,D]+[M,O,R,E]=[M,O,N,E,Y]),nl,fail;nl.
  25. :- write('WWWDOT - GOOGLE = DOTCOM'),nl,fukumen([D,O,T,C,O,M],[G,O,O,G,L,E],[W,W,W,D,O,T]),
  26. write([W,W,W,D,O,T]-[G,O,O,G,L,E]=[D,O,T,C,O,M]),nl,fail;true.
  27.  
Success #stdin #stdout #stderr 0.63s 6204KB
stdin
Standard input is empty
stdout
SEND + MORE = MONEY
[9, 5, 6, 7]+[1, 0, 8, 5]=[1, 0, 6, 5, 2]

WWWDOT - GOOGLE = DOTCOM
[7, 7, 7, 5, 8, 9]-[1, 8, 8, 1, 0, 6]=[5, 8, 9, 4, 8, 3]
[7, 7, 7, 5, 8, 9]-[1, 8, 8, 1, 0, 3]=[5, 8, 9, 4, 8, 6]
stderr