Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fast prime sort algorithm
(ns euler.problem7
(:use [euler.core]))
;; The algorithm for this is
;; - Start at the first 1000 primes
;; - If there are not enough primes then try again at 2000 etc
(defn get-nth-prime [n, max]
"Returns the nth prime"
(let [primes (sieve max)]
(if (< (count primes) n)
(recur n (* max 2))
(last (take n primes)))))
(defn solve [n]
(get-nth-prime n 1000))
(time (solve 10000))
;; "Elapsed time: 149.609 msecs"
;; 104729
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.