0

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.

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @TessellatingHeckler. I'm just getting a chance to revisit this now, since last week but you but answer helped me. I didn't realise my mistake in the spacing on my code. Do you know the best method to go to get my program to then list all prime numbers between 6 and my chosen value Y?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.