Search code examples
prologprimes

Prolog - Display prime numbers


I am new to Prolog programming so still learning. I'm tryingto create a program that accepts a value X and returns a list of all prime numbers between 6 and X. I'll also be later adding an error output if X is entered as 6 or lower.

I have come up with the below code so far to have the value of x entered, y as the first prime number and the counter as 1. Even this is not working, as I try to increment the values of Y. If i can get this working I plan to then add conditions to only show Y if it is a prime, by checking that it divides by itself and 1.

If anyone has any advice it would be greatly appreciated.

prime (X, Y, Counter) :-
    X >= Y,
    writeln(Y),
    Next is Y + Counter,
    prime (X, Next, Counter).

prime(X, Y, _) :-
     Y > X.

Solution

  • Remove the spaces in prime ( here and here:

    Screenshot of source code

    Then it works to call ?- prime(10, 3, 1). and have it count.