primes/primes_prolog.pl

29 lines
400 B
Prolog

% Prolog version of a Prime Number Finder
% haplo@mindstab.net
check(I,C,M,P) :-
P \= 0,
I > M.
check(I,C,M,P) :-
P \= 0,
NP is C mod I,
NI is I + 2,
check(NI,C,M,NP).
primes(M) :-
nl, primes(3,M,2).
primes(C,M,S) :- C > M.
primes(C,M,S) :-
check(3,C,S,1),
write(C), nl,
NC is C + 2,
NS is sqrt(NC),
primes(NC,M,NS).
primes(C,M,S) :-
NC is C + 2,
NS is sqrt(NC),
primes(NC,M,NS).