Skip to content

Instantly share code, notes, and snippets.

@ajmcmiddlin
Created March 24, 2013 09:41
Show Gist options
  • Save ajmcmiddlin/5231224 to your computer and use it in GitHub Desktop.
Save ajmcmiddlin/5231224 to your computer and use it in GitHub Desktop.
Exercise solutions from the March 2013 Melbourne Clojure Workshop. Exercises taken from the first 5 Project Euler problems.
(ns clj-workshop.exercises)
(defn multiple-of
[n]
#(= 0 (mod % n)))
(defn ex1
[]
(let [multiple-of-3? (multiple-of 3)
multiple-of-5? (multiple-of 5)]
(letfn [(multiple-of-3-or-5?
[n]
(or (multiple-of-3? n)
(multiple-of-5? n)))]
(apply + (filter multiple-of-3-or-5? (range 1 1000))))))
;; Stolen from Andy/Logan
(defn fib
([a b]
(cons a (lazy-seq (fib b (+ b a)))))
([]
(fib 1 1)))
(defn ex2
[]
(apply + (filter even? (take-while #(< % 4000000) (fib)))))
(defn ex3
[]
(let [products-of-3-digit-nums (for [x (range 100 1001) y (range 100 1001)] (* x y))
palindrome? (fn [n] (= (str n) (clojure.string/reverse (str n))))]
(apply max (filter palindrome? products-of-3-digit-nums))))
(defn ex4
[]
;; Numbers from 11-20 include multiples of all numbers from 1-10, so only need to check this upper range
(let [multiple-checks (map multiple-of (range 11 21))
multiple-of-all-1-to-20? (fn [n]
(every? #(% n) multiple-checks))]
(first (drop-while #(not (multiple-of-all-1-to-20? %)) (rest (range))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment