Created
November 13, 2012 14:28
-
-
Save Kuchitama/4066016 to your computer and use it in GitHub Desktop.
Solved Project Euler by clojure
This file contains hidden or 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
(apply + | |
(filter #(or (= (mod % 3) 0) (= (mod % 5) 0)) | |
(range 1 1000))) |
This file contains hidden or 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 [a b] (cons a (lazy-seq (fib b (+ b a))))) | |
(apply + | |
(filter even? | |
(take-while | |
#(< % 4000000) | |
(fib 1 2)))) |
This file contains hidden or 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 primitive? [n] | |
(if (< n 2) | |
false | |
(#(if (zero? (mod %1 %2)) | |
(= %1 %2) | |
(recur %1 (inc %2))) n 2))) | |
((fn [i n] (if (<= i n) | |
(let [mod-zero? (= (mod n i) 0) ret (/ n i)] | |
(if (and mod-zero? (is-primitive ret)) | |
(long ret) | |
(if mod-zero? | |
(recur 2 ret) | |
(recur (inc i) n)))))) 2 600851475143) |
This file contains hidden or 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 palindrome? [n] (let [n-str (str "" n)] | |
(= n-str | |
(clojure.string/join (reverse n-str))))) | |
(defn prod-3-digit? [n] | |
(#(if (< %2 1000) | |
(if (and (= 0 (mod %1 %2)) | |
(< 99 (/ %1 %2)) (> 1000 (/ %1 %2))) | |
true | |
(recur %1 (inc %2))) | |
false) n 100)) | |
(first (filter #(and (palindrome? %) (prod-3-digit? %)) | |
(range (* 1000 1000) (* 99 99) -1))) |
This file contains hidden or 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 primitive? [n] | |
(if (< n 2) | |
false | |
(#(if (zero? (mod %1 %2)) | |
(= %1 %2) | |
(recur %1 (inc %2))) n 2))) | |
(defn get-max-pow [limit n] (#(if (> (* % %2) limit) % | |
(recur (* % %2) %2)) n n)) | |
((fn [n] (let [nums (filter primitive? (range 2 (inc n)))] | |
(reduce | |
(fn [a b] (* a (get-max-pow n b))) | |
1 nums))) 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment