Skip to content

Instantly share code, notes, and snippets.

@futureperfect
Created December 12, 2011 04:14
Show Gist options
  • Save futureperfect/1464806 to your computer and use it in GitHub Desktop.
Save futureperfect/1464806 to your computer and use it in GitHub Desktop.
Generate prime numbers in lisp
(defun divisible-by (number list)
"return true if divisible by an element in the list"
(loop for i in list
thereis (= (mod number i) 0)))
(defun prime-list-generator (current max-value prime-list)
"business end of p10"
(if (= current max-value)
prime-list
(if (divisible-by current prime-list)
(prime-list-generator (+ current 1) max-value prime-list)
(prime-list-generator (+ current 1) max-value (nconc prime-list (list current))))))
(defun p10 (n)
"Find the sum of all the primes below n"
(time (reduce #'+ (prime-list-generator 2 n '()))))
(p10 2000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment