Skip to content

Instantly share code, notes, and snippets.

@SamirTalwar
Created September 15, 2019 17:53
Show Gist options
  • Save SamirTalwar/41304ce5dd38b4a7e4a25a617c6afc98 to your computer and use it in GitHub Desktop.
Save SamirTalwar/41304ce5dd38b4a7e4a25a617c6afc98 to your computer and use it in GitHub Desktop.
A prime number detector in Prolog that caches intermediate results.
:- dynamic(prime/1).
prime(P) :-
P > 1,
\+ not_prime(P),
asserta(prime(P) :- !).
not_prime(P) :-
SqrtP is round(sqrt(P)),
between(2, SqrtP, N),
prime(N),
P rem N =:= 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment