I am new to prolog. I was writing a code with Goldbach’s Conjecture problem which I have to list all possible groups of one even number. I found a code like this:
is_prime(2).
is_prime(3).
is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+ has_factor(P,3).
has_factor(N,L) :- N mod L =:= 0.
has_factor(N,L) :- L * L < N, L2 is L + 2, has_factor(N,L2).
goldbach(4,[2,2]) :- !.
goldbach(N,L) :-
N mod 2 =:= 0,
N > 4,
goldbach(N,L,3).
goldbach(N,[P,Q],P) :-
Q is N - P,
is_prime(Q), P < Q.
goldbach(N,L,P) :-
P < N,
next_prime(P,P1),
goldbach(N,L,P1).
next_prime(P,P1) :- P1 is P + 2, is_prime(P1), !.
next_prime(P,P1) :- P2 is P + 2, next_prime(P2,P1).
However when I execute the program, the program prints the first group successfully, but then comes up with error.
1 ?- goldbach(28, L).
L = [5, 23]
ERROR: Type error: `character_code' expected, found `-1' (an integer)
ERROR: In:
ERROR: [11] char_code(_4206,-1)
ERROR: [10] '$in_reply'(-1,'?h') at c:/program files/swipl/boot/init.pl:779
I do not know how this error occurs, and I look for information in Internet but get nothing. Does someone have any idea? Thank you for your answering.
After Prolog prints the first answer, it waits for your input to tell it whether you are interested in more answers. To a first approximation, valid keys to press are ; (semicolon) or Space or n if you want more answers, or . (period) or Enter or y if you are not interested in any more answers.
It looks like you are using some other, unexpected, key or key combination. If you are trying to type ; but your keyboard doesn't have a single key for it, try Space or n instead.
I can successfully get several answers out of the code you posted by pressing ; after each answer:
?- goldbach(28, L).
L = [5, 23] ;
L = [11, 17] ;
false.
Edit: As you are on Windows, the answer from After the first answer, Prolog shows the error "char_code/2: Cannot represent due to 'character_code'" to use swipl-win.exe
instead of swipl.exe
might also be useful.