Skip to content

Instantly share code, notes, and snippets.

@acgetchell
Created December 1, 2011 13:11
Show Gist options
  • Save acgetchell/1416617 to your computer and use it in GitHub Desktop.
Save acgetchell/1416617 to your computer and use it in GitHub Desktop.
Getting the 10,001st Prime Number
;; 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