Created
November 3, 2008 04:18
-
-
Save 53cr/21795 to your computer and use it in GitHub Desktop.
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
;; 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