Primes by Trial Division - Common LISP

25 September 2016

Changes from the INTERLISP version:

Common LISPINTERLISP
lowercaseUPPERCASE
+PLUS
<LESSP
modREMAINDER
defunDEFINEQ
block nilPROG NIL
mapcar func listMAPCAR 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.

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026

Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Primes Benchmark Source

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

Primes Benchmark Results

Other LISPs

Cambridge LISP

Common LISP

Domain Lisp

Golden Common LISP

INTERLISP

Interlisp/65

LISP/80

Pearl LISP

Scheme R³

XLISP