Skip to content

Instantly share code, notes, and snippets.

@agrif
Created July 15, 2012 04:05
Show Gist options
  • Save agrif/3114920 to your computer and use it in GitHub Desktop.
Save agrif/3114920 to your computer and use it in GitHub Desktop.
;; returns t if the number is prime, nil otherwise
(defun is-prime (n prev-primes)
(not (find-if
(lambda (p)
(eq (mod n p) 0))
prev-primes)))
;; returns all primes between start and end - 1 inclusive
;; prev-primes should be a list of primes before start, if start > 2
(defun primes-between (start end &optional prev-primes)
;; check for the end condition first
(if (eq start end)
;; this is it, return the primes found so far
prev-primes
(progn
;; check if start is prime
(if (is-prime start prev-primes)
;; it is! so push it onto previous primes
(push start prev-primes))
;; recurse
(primes-between (+ 1 start) end prev-primes))))
;; test -- all primes between 2 and 100
(format 't "~a"
(primes-between 2 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment