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
).
prolog
add a comment |
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
).
prolog
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 forX
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
add a comment |
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
).
prolog
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
prolog
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 forX
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
add a comment |
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 forX
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 forX
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