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