Skip to content

Instantly share code, notes, and snippets.

@53cr
Created November 3, 2008 04:18
Show Gist options
  • Save 53cr/21795 to your computer and use it in GitHub Desktop.
Save 53cr/21795 to your computer and use it in GitHub Desktop.
;; The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
;; Find the sum of all the primes below two million.
(defn is-prime [n]
(loop [d (int (Math/sqrt n))]
(if (= d 1)
true
(if (= 0 (rem n d))
false
(recur (dec d))))))
(time (loop [n 2, acc 0]
(if (> n 2000000)
acc
(if (is-prime n)
(recur (inc n) (+ acc n))
(recur (inc n) acc)))))
; "Elapsed time: 134703.35 msecs"
; 142913828922
;; Damn it Github, give me Clojure syntax hightlighting. Oh well. Scheme works ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment