Last active
April 11, 2023 08:06
-
-
Save thinkphp/5348581 to your computer and use it in GitHub Desktop.
Test if a number is prime or not in Common LISP.
This file contains 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
;Test if a number is prime or NOT. | |
;Adrian Statescu <mergesortv@gmail.com> | |
;License | |
;MIT | |
;Cheers! | |
(defun forall(L P) | |
(if (null L) | |
T | |
(and (FUNCALL P (cAR L)) (forall (CDR L) P) ) | |
) | |
) | |
(defun nums(start stop) | |
(setf L nil) | |
(loop ( when(> start stop) (return (reverse L)) ) | |
(setf L (cons start L) ) | |
(incf start) | |
) | |
) | |
(defun prime(N) | |
(forall (nums 2 (floor (sqrt N)) ) | |
#'(lambda (d) (not (= (MOD N d) 0))) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment