Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Last active April 11, 2023 08:06
Show Gist options
  • Save thinkphp/5348581 to your computer and use it in GitHub Desktop.
Save thinkphp/5348581 to your computer and use it in GitHub Desktop.
Test if a number is prime or not in Common LISP.
;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