Skip to content

Instantly share code, notes, and snippets.

@kzar
Created November 26, 2009 06:29
Show Gist options
  • Save kzar/243280 to your computer and use it in GitHub Desktop.
Save kzar/243280 to your computer and use it in GitHub Desktop.
; 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