Created
January 23, 2012 07:38
-
-
Save biesnecker/1661415 to your computer and use it in GitHub Desktop.
Snippets for Project Euler 1-3 in Clojure
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
(defn prog-sum | |
"Calculates the sum of an arithmetic progression" | |
([n a] (prog-sum n a a)) | |
([n a d] (/ (* n (+ (* 2 a) (* (dec n) d))) 2))) | |
(defn euler-1 [n f1 f2] | |
(let [ | |
b (dec n) | |
d1 (quot b f1) | |
d2 (quot b f2) | |
f3 (* f1 f2) | |
d3 (quot b f3)] | |
(- (+ (prog-sum d1 f1) (prog-sum d2 f2)) (prog-sum d3 f3)))) |
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
(defn fib-seq [] | |
((fn rfib [a b] (cons a (lazy-seq (rfib b (+ a b))))) 0 1)) | |
(defn euler-2 [] | |
(reduce + (filter even? (take-while #(< % 4000000) (fib-seq))))) |
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
(defn prime? [x] | |
(cond | |
(< x 2) false | |
(= x 2) true | |
:else (not (some zero? | |
(map #(rem x %) (range 2 (inc (int (Math/sqrt x))))))))) | |
(defn euler-3 [n] | |
(let [sqrt (int (Math/sqrt n))] | |
(first (filter #(and (zero? (rem n %)) (prime? %)) (range sqrt 0 -1))))) |
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
(defn prime-factors [n] | |
(loop [factors [] n n div 2] | |
(cond | |
(= n 1) factors | |
(zero? (rem n div)) (recur (conj factors div) (/ n div) div) | |
(> (* div div) n) (conj factors n) | |
:else (recur factors n (inc div))))) |
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
(defn prime? [x] | |
(cond | |
(< x 2) false | |
(= x 2) true | |
:else (not (some zero? | |
(map #(rem x %) (range 2 (inc (int (Math/sqrt x))))))))) |
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
(defn prog-sum | |
"Calculates the sum of an arithmetic progression" | |
([n a] (prog-sum n a a)) | |
([n a d] (/ (* n (+ (* 2 a) (* (dec n) d))) 2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment