Created
December 1, 2011 13:11
-
-
Save acgetchell/1416617 to your computer and use it in GitHub Desktop.
Getting the 10,001st Prime Number
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
;; From http://clojure-euler.wikispaces.com/Problem+007 | |
;; Useful examples on how easy it is to do number processing in Clojure | |
;; achim_p's solution, understandable and REPLicable | |
(defn div? [n d] | |
(= 0 (rem n d))) | |
(defn smallest-prime-factor [number] | |
(loop [n number d 2] | |
(cond (> d (int (Math/sqrt number))) n | |
(= n d) n | |
(div? n d) d | |
true (recur n (inc d))))) | |
(def primes (lazy-cat '(2 3) | |
(filter #(= %1 (smallest-prime-factor %1)) | |
(take-nth 2 (iterate inc 5))))) | |
(nth primes 10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment