Created
February 23, 2010 15:30
-
-
Save hroi/312305 to your computer and use it in GitHub Desktop.
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
; Problem 7 | |
; | |
; By listing the first six prime numbers: 2, 3, 5, 7, 11, | |
; and 13, we can see that the 6th prime is 13. | |
; | |
; What is the 10001st prime number? | |
(defn prime? [x] | |
(cond (< x 2) false | |
(= x 2) true | |
(even? x) false | |
:else (let [boundary (inc (int (Math/sqrt x))) | |
candidates (range 3 boundary 2)] | |
(empty? (drop-while #(pos? (mod x %)) candidates))))) | |
(defn nth-prime [n] ; get n-th prime number | |
(cond (= n 1) 2 | |
(= n 2) 3 | |
(pos? n) | |
(loop [x 3 count 2] | |
(if (prime? x) | |
(if (= count n) | |
x | |
(recur (+ 2 x) (inc count))) | |
(recur (+ 2 x) count))))) | |
(def answer (future (nth-prime 10001))) ; Takes long :-) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment