; create prime list from 2 thru n. ; primetest will return an integer x if x is prime; otherwise it returns nil. ; mapcar'ing primetest to integer list, then deleting the nils, results in ; desired answer. (defun primes (n) (cond ((<= n 1) nil) ((equal n 2) '(2)) (t (cons 2 (filternils (mapcar 'primetest (nlist 3 n))))) ) ) (defun nlist (a b) (cond ((>= a b) (list b)) (t (cons a (nlist (1+ a) b) ) ) ) ) ;;; simple iterative testing for a prime number: mod all integers between 2 and sqrt(n) (defun primetest (n) (let ((end (ceiling (sqrt n))) (cnt 2) ) (loop (cond ((zerop (mod n cnt)) (return nil)) ((equal cnt end) (return n)) ) (setq cnt (1+ cnt)) ) ) ) ;;; delete nils from list l (defun filternils (l) (cond ((null l) nil) ((null (car l)) (filternils (cdr l))) (t (cons (car l) (filternils (cdr l)))) ) )