Created
June 12, 2012 13:17
-
-
Save mikelikesbikes/2917459 to your computer and use it in GitHub Desktop.
Germain Primes
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
;; http://stackoverflow.com/questions/1590716/clojure-prime-numbers-lazy-sequence | |
(def primes | |
(let [primes (atom [])] | |
(for [n (iterate inc 2) | |
:when (not-any? #(zero? (rem n %)) | |
(filter #(<= % (Math/sqrt n)) | |
@primes))] | |
(do (swap! primes conj n) | |
n)))) | |
;; http://blog.marrowboy.co.uk/2011/02/03/implementing-is-prime-n-in-clojure-using-uncle-bobs-improved-transformation-priority-list | |
(defn is-prime? [n] | |
(if (= n 1) false | |
(every? false? | |
(map #(= 0 (mod n %1)) (range 2 n))))) | |
;; Hey look... finally some code I wrote. I know... it's wicked sweet. | |
(def germain-primes | |
(filter #(is-prime? (inc (* 2 %))) primes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment