Skip to content

Instantly share code, notes, and snippets.

@olayinkasf
Created December 5, 2014 17:33
Show Gist options
  • Save olayinkasf/8e0a8db8b665162ef21d to your computer and use it in GitHub Desktop.
Save olayinkasf/8e0a8db8b665162ef21d to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
% Author: [email protected]
% Date: 12/5/2014
add(1) :- !.
add(X) :- asserta(prime(X)), Y is X-1, add(Y).
do_sieve(X, Y) :- ( X \== Y -> retract(prime(Y)) ; true), Z is Y + X, do_sieve(X,Z).
sieve :- prime(T), do_sieve(T,T).
sieve(N) :- ( add(N), sieve ); forall(prime(X), writeln(X)), true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment