Sha256: 65c2ae7dc3a0efc6130fb25b0e179e7b3530f8133979771148f07d0c685d2a36

Contents?: true

Size: 832 Bytes

Versions: 289

Compression:

Stored size: 832 Bytes

Contents

(ns nth-prime)

(defn sqrt
  "Wrapper around java's sqrt method."
  [number]
  (int (Math/ceil (Math/sqrt number))))

(defn divides?
  "Helper function to decide if a number is evenly divided by divisor."
  [number divisor]
  (zero? (mod number divisor)))

(defn- prime-by-trial-division?
  "Simple trial division prime check."
  [number]
  (empty? (for [n (range 3 (inc (sqrt number)) 2) :when (divides? number n)] n)))

(defn prime? [number]
  (or (= 2 number)
      (and (odd? number) (prime-by-trial-division? number))))

(defn next-prime [start]
  (loop [n (inc start)]
    (if (prime? n)
      n
      (recur (inc n)))))

(def primes (iterate next-prime 1))

(defn nth-prime [index]
  (when-not (pos? index)
    (throw (IllegalArgumentException. "nth-prime expects a positive integer for an argument")))
  (nth primes index))

Version data entries

289 entries across 289 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.179 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.178 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.177 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.176 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.175 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.174 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.173 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.172 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.171 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.170 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.169 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.167 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.166 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.165 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.164 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.163 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.162 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.161 tracks/clojure/exercises/nth-prime/src/example.clj
trackler-2.2.1.160 tracks/clojure/exercises/nth-prime/src/example.clj