Created
September 15, 2019 17:53
-
-
Save SamirTalwar/41304ce5dd38b4a7e4a25a617c6afc98 to your computer and use it in GitHub Desktop.
A prime number detector in Prolog that caches intermediate results.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- 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