Я закончил общую арифметическую головоломку в Прологе. Мне нужно знать, как получить все возможные решения для каждой конкретной головоломки и поместить их в отдельный выходной текстовый файл. Буду признателен за любую помощь.
It outputs to a text file, but I want the format to be:
puzzle([A,M]+[P,M]=[D,A,Y]).
First Solution is: [2,5] + [9,5] = [1,2,0]
There are total of NN solutions output to the file: puzzle2sol1.txt
where NN is the amount of solutions.
:- use_module(library(clpfd)).
sumhelp([],[],[],C,C,Digits,Digits).
sumhelp([D1|N1],[D2|N2],[D|N],C1,C,Digs1,Digs):-
sumhelp(N1,N2,N,C1,C2,Digs1,Digs2),
digits(D1,D2,C2,D,C,Digs2,Digs).
digits(D1,D2,C1,D,C,Digs1,Digs):-
rem(D1,Digs1,Digs2),
rem(D2,Digs2,Digs3),
rem(D,Digs3,Digs),
S #= D1 + D2 + C1,
D #= S mod 10,
C #= S // 10.
rem(A,L,L):- nonvar(A),!.
rem(A,[A|L],L).
rem(A,[B|L],[B|L1]):-
rem(A,L,L1).
puzzle(A + B = C) :-
tell('output.txt'),
(
sumhelp([0|A], [0|B], C, 0, 0, [0,1,2,3,4,5,6,7,8,9],_),writeln([A + B = C]),
fail
;
told
).