Prolog CryptoArithmetic











up vote
0
down vote

favorite












I have completed the general arithmetic puzzle in Prolog. I need to know how to get all the possible solutions for each particular puzzle and put them in a separate output text file. I would appreciate any help.



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
).









share|improve this question
























  • Have you read the Prolog documentation for file I/O? Exactly where are you stuck regarding how to incorporate the file I/O?
    – lurker
    Nov 11 at 11:40












  • Yes, I have read that. So I created a cryptoarithmetic puzzle. And each particular puzzle can have multiple solutions which you can ask for with a semicolon (;), but I want all the solutions to be listed together in an output text file. Do you know how to do that?
    – Rob
    Nov 11 at 16:25










  • Here's a template. You can modify it as needed for your predicate: open('foo.txt', write, Out), (foo(X), writeln(Out, X), fail ; close(Out)). Here, foo(X) can have multiple solutions for X and each will individually be written to the output stream. This is called a failure driven loop in Prolog. SWI Prolog has output formatting as well, which you can find in the manual.
    – lurker
    Nov 11 at 19:12

















up vote
0
down vote

favorite












I have completed the general arithmetic puzzle in Prolog. I need to know how to get all the possible solutions for each particular puzzle and put them in a separate output text file. I would appreciate any help.



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
).









share|improve this question
























  • Have you read the Prolog documentation for file I/O? Exactly where are you stuck regarding how to incorporate the file I/O?
    – lurker
    Nov 11 at 11:40












  • Yes, I have read that. So I created a cryptoarithmetic puzzle. And each particular puzzle can have multiple solutions which you can ask for with a semicolon (;), but I want all the solutions to be listed together in an output text file. Do you know how to do that?
    – Rob
    Nov 11 at 16:25










  • Here's a template. You can modify it as needed for your predicate: open('foo.txt', write, Out), (foo(X), writeln(Out, X), fail ; close(Out)). Here, foo(X) can have multiple solutions for X and each will individually be written to the output stream. This is called a failure driven loop in Prolog. SWI Prolog has output formatting as well, which you can find in the manual.
    – lurker
    Nov 11 at 19:12















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have completed the general arithmetic puzzle in Prolog. I need to know how to get all the possible solutions for each particular puzzle and put them in a separate output text file. I would appreciate any help.



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
).









share|improve this question















I have completed the general arithmetic puzzle in Prolog. I need to know how to get all the possible solutions for each particular puzzle and put them in a separate output text file. I would appreciate any help.



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
).






prolog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:12

























asked Nov 11 at 3:47









Rob

12




12












  • Have you read the Prolog documentation for file I/O? Exactly where are you stuck regarding how to incorporate the file I/O?
    – lurker
    Nov 11 at 11:40












  • Yes, I have read that. So I created a cryptoarithmetic puzzle. And each particular puzzle can have multiple solutions which you can ask for with a semicolon (;), but I want all the solutions to be listed together in an output text file. Do you know how to do that?
    – Rob
    Nov 11 at 16:25










  • Here's a template. You can modify it as needed for your predicate: open('foo.txt', write, Out), (foo(X), writeln(Out, X), fail ; close(Out)). Here, foo(X) can have multiple solutions for X and each will individually be written to the output stream. This is called a failure driven loop in Prolog. SWI Prolog has output formatting as well, which you can find in the manual.
    – lurker
    Nov 11 at 19:12




















  • Have you read the Prolog documentation for file I/O? Exactly where are you stuck regarding how to incorporate the file I/O?
    – lurker
    Nov 11 at 11:40












  • Yes, I have read that. So I created a cryptoarithmetic puzzle. And each particular puzzle can have multiple solutions which you can ask for with a semicolon (;), but I want all the solutions to be listed together in an output text file. Do you know how to do that?
    – Rob
    Nov 11 at 16:25










  • Here's a template. You can modify it as needed for your predicate: open('foo.txt', write, Out), (foo(X), writeln(Out, X), fail ; close(Out)). Here, foo(X) can have multiple solutions for X and each will individually be written to the output stream. This is called a failure driven loop in Prolog. SWI Prolog has output formatting as well, which you can find in the manual.
    – lurker
    Nov 11 at 19:12


















Have you read the Prolog documentation for file I/O? Exactly where are you stuck regarding how to incorporate the file I/O?
– lurker
Nov 11 at 11:40






Have you read the Prolog documentation for file I/O? Exactly where are you stuck regarding how to incorporate the file I/O?
– lurker
Nov 11 at 11:40














Yes, I have read that. So I created a cryptoarithmetic puzzle. And each particular puzzle can have multiple solutions which you can ask for with a semicolon (;), but I want all the solutions to be listed together in an output text file. Do you know how to do that?
– Rob
Nov 11 at 16:25




Yes, I have read that. So I created a cryptoarithmetic puzzle. And each particular puzzle can have multiple solutions which you can ask for with a semicolon (;), but I want all the solutions to be listed together in an output text file. Do you know how to do that?
– Rob
Nov 11 at 16:25












Here's a template. You can modify it as needed for your predicate: open('foo.txt', write, Out), (foo(X), writeln(Out, X), fail ; close(Out)). Here, foo(X) can have multiple solutions for X and each will individually be written to the output stream. This is called a failure driven loop in Prolog. SWI Prolog has output formatting as well, which you can find in the manual.
– lurker
Nov 11 at 19:12






Here's a template. You can modify it as needed for your predicate: open('foo.txt', write, Out), (foo(X), writeln(Out, X), fail ; close(Out)). Here, foo(X) can have multiple solutions for X and each will individually be written to the output stream. This is called a failure driven loop in Prolog. SWI Prolog has output formatting as well, which you can find in the manual.
– lurker
Nov 11 at 19:12



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245667%2fprolog-cryptoarithmetic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245667%2fprolog-cryptoarithmetic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ