primes/primes.prolog.pl

35 lines
409 B
Perl
Raw Normal View History

2011-03-08 07:01:27 +00:00
% prolog
check(I,C,M,P) :-
P \= 0,
I > M,
write(C), nl.
check(I,C,M,P) :-
I =< M,
P \= 0,
NP is C mod I,
NI is I + 2,
check(NI,C,M,NP).
check(I,C,M,0).
primes(M) :-
nl, primes(3,M,2,4).
primes(C,M,S,T) :-
C =< M,
C > T,
NS is S + 1,
NT is NS * NS,
check(3,C,NS,1),
NC is C + 2,
primes(NC,M,NS,NT).
primes(C,M,S,T) :-
C =< M,
C =< T,
check(3,C,S,1),
NC is C + 2,
primes(NC,M,S,T).