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.