Skip to content

Instantly share code, notes, and snippets.

@owainlewis
Created April 5, 2012 12:41
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 owainlewis/2310781 to your computer and use it in GitHub Desktop.
Save owainlewis/2310781 to your computer and use it in GitHub Desktop.
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