1

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.

false
  • 10,264
  • 13
  • 101
  • 209
BonWaGG
  • 43
  • 1
  • 4

1 Answers1

1

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.

Isabelle Newbie
  • 9,258
  • 1
  • 20
  • 32
  • 1
    The unexpected key combination is "end-of-file", C-d or whatever that is on Win-dow. – false May 27 '17 at 10:59
  • thank you for your help. all the problems are solved. I am really new to prolog so I do not know this. Can I ask that if this code were executed in other OS, can it be all right when using swipl.exe? – BonWaGG May 27 '17 at 11:04
  • It works fine under Linux using the standard `swipl` program. The difference between those programs is specific to Windows. Don't worry about it, just use whatever works for you. – Isabelle Newbie May 27 '17 at 11:17
  • @BonWaGG information on how to use Prolog at the command line is in the Prolog documentation. If you're new to Prolog, you should read through the introductory material in the Prolog manual. – lurker May 27 '17 at 11:50
  • @IsabelleNewbie thank you for your explanation!! – BonWaGG May 27 '17 at 18:12
  • @lurker Thanks, I think I should figure it out as soon as posssible. – BonWaGG May 27 '17 at 18:13