Why does this program does not work in Prolog ?
% Goldbach's conjecture.
% Goldbach's conjecture says that every positive even number greater
% than 2 is the sum of two prime numbers. Example: 28 = 5 + 23.
:- ensure_loaded(p31).
% goldbach(N,L) :- L is the list of the two prime numbers that
% sum up to the given N (which must be even).
% (integer,integer) (+,-)
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), !.
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).
First off, I had to remove the code line :- ensure_loaded(p31). Otherwise marked an error saying that does not exist.
Second, when I run it on SWI-Prolog screen with ?-goldbach(4,X,Y). marked an error that says:
ERROR: Arguments are not sufficiently instantiated
Why?
Could someone please help me to fix the program?
Thank you.
p31? Note thatis/2is only for arithmetic evaluation when all of the variables of the 2nd argument are known numeric values. Try using CLP(FD). - lurkergoldbach(4, X, Y)? The code comment clearly indicates that the top level isgoldbach/2. You should be callinggoldbach(4, L). - lurkergoldbach(4, X, Y)will give you the two primes inXandY, but this is not so.goldbach(4, X)will give you the two primes in a list inX. Prolog is quite different from other languages and you need to start reading an introduction to logic programming and actively solving simpler exercises. What you are looking at is a solved exercise in the middle of a complex sequence of exercises with dependencies between them. - Rob Blancogoldbach(4, L). If you have a different specific question about a specific issue with programming in Prolog, you should post it separately. If you just want a 3-argument version of this program, you could write,my_goldbach(N, X, Y) :- goldbach(N, [X,Y]).and callmy_goldbach(4, X, Y).- lurker