Last active
January 19, 2017 08:19
-
-
Save craftybones/a8748d39a480034f0fffc08a99915301 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defn closest-integer-sqrt [x] | |
(-> x Math/sqrt Math/floor int)) | |
(defn prime-candidates-above-3 [x] | |
(lazy-seq (list* (dec (* 6 x)) (inc (* 6 x)) (prime-candidates-above-3 (inc x))))) | |
(def prime-candidates | |
(lazy-seq (list* 2 3 (prime-candidates-above-3 1)))) | |
(def none (comp not some)) | |
(defn divisible-by? [x y] | |
(zero? (rem x y))) | |
(defn divisibility-check [x] | |
(partial none (partial divisible-by? x))) | |
(defn prime-candidates-below [x] | |
(take-while (partial >= x) prime-candidates)) | |
(defn is-prime? [x] | |
(let [threshold (closest-integer-sqrt x) | |
x-not-divisible-by? (divisibility-check x)] | |
(x-not-divisible-by? (prime-candidates-below threshold)))) | |
(def primes (filter is-prime? prime-candidates)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment