Reputation: 1
I'm trying to learn Prolog and I've found an example where I need to implement a program to check whether a number is prime or not with a single predicate.
The logic I'm trying to follow is to make a recursive rule to divide by all the number less than that predicate till it reaches the base case which is either X>2
because 0
and 1
aren't primes and divisible by itself
My code till now is :
isPrime(2).
isPrime(X):-
X>2, %0,1 aren't primes
1 is mod(X,2),
Can someone help ?
Upvotes: 0
Views: 1695
Reputation: 22803
It's pretty easy provided you don't care about efficiency.
isPrime(X) :-
X > 1,
succ(X0, X),
\+ (between(2, X0, N), 0 is X mod N).
:)
Upvotes: 3