public
Created

Exercise solutions from the March 2013 Melbourne Clojure Workshop. Exercises taken from the first 5 Project Euler problems.

  • Download Gist
exercises.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
(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))))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.