Primes by Trial Division - Common LISP
25 September 2016
Changes from the INTERLISP version:
Common LISP | INTERLISP |
---|---|
lowercase | UPPERCASE |
+ | PLUS |
< | LESSP |
mod | REMAINDER |
defun | DEFINEQ |
block nil | PROG NIL |
mapcar func list | MAPCAR list func |
Variant A
(defun primes (n) (prog (count primes) (setq count 3) (setq primes (list 2)) NEXT (cond ((< (length primes) n) (cond ((member 0 (mapcar #'(lambda (x) (mod count x)) primes)) nil) (T (nconc primes (list count))))) (T (return primes))) (setq count (+ count 1)) (go NEXT)))
Variant B
(defun divp (n list) (block nil (mapc #'(lambda (p) (cond ((zerop (mod n p)) (return-from divp T)))) list) (return-from divp nil))) (defun primes (n) (prog (count primes) (setq count 3) (setq primes (list 2)) NEXT (cond ((< (length primes) n) (cond ((divp count primes) nil) (T (nconc primes (list count))))) (T (return primes))) (setq count (+ count 1)) (go NEXT)))
Variant C
For OpenGenera, Set Stack Size Control 49152 with n=10000.
(defun divp (n list) (cond ((numberp (car list)) (cond ((zerop (mod n (car list))) T) ((divp n (cdr list)) T)))))
primes definition same as Variant B.