You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

34 lines
409 B

% 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).