Created
November 26, 2009 06:29
-
-
Save kzar/243280 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
; Problem 1 | |
(apply + (filter #(or (= 0 (mod % 5)) (= 0 (mod % 3))) (range 1000))) | |
; Problem 2 | |
(defn fib | |
([] (fib 0 1)) | |
([n-2 n-1] | |
(lazy-seq (cons n-1 (fib n-1 (+ n-2 n-1)))))) | |
(reduce + (take-while #(< % 4000000) (filter even? (fib)))) | |
; Problem 3 | |
(defn prime-factors [i] | |
(or (if-let [smallest-factor (first (filter #(= 0 (mod i %)) (range 2 (. Math sqrt i))))] | |
(cons smallest-factor (prime-factors (/ i smallest-factor)))) | |
(list i))) | |
(prime-factors 600851475143) | |
; Problem 4 | |
(defn palindrome? [i] | |
(when (even? (count (str i))) | |
(let [[first second] (split-at (/ (count (str i)) 2) (str i))] | |
(when (= first (reverse second)) | |
i)))) | |
(apply max (filter palindrome? (for [x (range 1000) y (range 1000)] (* x y)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment