Skip to content

Instantly share code, notes, and snippets.

@dagda1
Created February 7, 2015 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagda1/41141f53b50b06c98094 to your computer and use it in GitHub Desktop.
Save dagda1/41141f53b50b06c98094 to your computer and use it in GitHub Desktop.
(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