August 19, 2024

P31 - Determine whether a given integer number is prime

Example:* (is-prime 7)T

lisp

;;; Determines if a number is prime
(defun is-prime (n)
(and
(> n 1)
(null (proper-divisors n))
)
)

;;; Returns a list with the proper divisors of N
;;; Proper divisors: are the divisors other than 1 and N
(defun proper-divisors (n)
(proper-divisors-up-to n (floor n 2))
)

;;; Returns a list with the proper divisors of N
;;; that do not exceed M (defun proper-divisors-up-to (n m) (cond ((<= m 1) ()) ((= 0 (mod n m)) (cons m (proper-divisors-up-to n (1- m)))) (t (proper-divisors-up-to n (1- m))) ) )
Be first to comment
Leave a reply