Created
July 15, 2012 04:05
-
-
Save agrif/3114920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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