Created
February 7, 2015 22:02
-
-
Save dagda1/41141f53b50b06c98094 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
(ns scratch.core | |
(require [clojure.string :as str :only (split-lines join)])) | |
(defn is-prime? [n] | |
(or (= 2 n) | |
(not-any? #(= 0 (mod n %)) (range 3 (inc (Math/sqrt n)) 2)))) | |
(defn nth-prime [n] | |
(last (take n (filter #(is-prime? %) (cons 2 (iterate (partial + 2) 3)))))) | |
(defn print-primes [[x & xs]] | |
(prn x) | |
(if (seq xs) | |
(recur xs))) | |
(let [input (slurp *in*) | |
ranks (rest (map read-string (str/split-lines input))) | |
primes (map nth-prime ranks)] | |
(time (print-primes primes))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment