Skip to content

Instantly share code, notes, and snippets.

@ponkore
Created April 29, 2012 11:46
Show Gist options
  • Save ponkore/2549731 to your computer and use it in GitHub Desktop.
Save ponkore/2549731 to your computer and use it in GitHub Desktop.
Project Euler Problem 10
;;; The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
;;; Find the sum of all the primes below two million.
(defn primes [n]
(loop [prime-list '(2) search-list (take-while #(< % n) (iterate inc 2))]
(let [prime-list-max (first prime-list)
search-list-next (filter #(not (zero? (mod % prime-list-max))) search-list)]
(if (< n (* prime-list-max prime-list-max))
(sort (concat prime-list search-list-next))
(recur (conj prime-list (first search-list-next)) search-list-next)))))
(def primes-2m (primes (* 200 10000)))
(count primes-2m)
;;; => 148933
(apply + primes-2m)
;;; => ans.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment