Skip to content

Instantly share code, notes, and snippets.

@pbadenski
Created May 7, 2010 18:16
Show Gist options
  • Save pbadenski/393806 to your computer and use it in GitHub Desktop.
Save pbadenski/393806 to your computer and use it in GitHub Desktop.
Project Euler 5
(use 'clojure.contrib.test-is)
(use 'clojure.contrib.trace)
(with-test
(defn prime-factorials [n]
(if (= n 1)
'()
(if-let [
divisor
(some
#(when (zero? (mod n %)) %)
(range 2 (inc n)))]
(cons divisor (prime-factorials (/ n divisor))))))
(are (= (prime-factorials _1) _2)
1 []
2 [2]
3 [3]
4 [2 2]
5 [5]
8 [2 2 2]))
(with-test
(defn but [seq elem]
(if-not (some #{elem} seq)
seq
(loop [result [] seq seq]
(if (= (first seq) elem)
(concat result (rest seq))
(recur (conj result (first seq)) (rest seq))))))
(are (= (but _1 _2) _3)
[1] 1 []
[1 2] 1 [2]
[1 2] 0 [1 2]
[] 1 []
[1 2 3] 3 [1 2]))
(with-test
(defn difference [seq1 seq2]
(if (empty? seq2)
seq1
(difference (but seq1 (first seq2)) (rest seq2))))
(are (= (difference _1 _2) _3)
[] [] []
[] [1] []
[1] [1] []
[1] [] [1]
[1 1] [1] [1]))
(reduce
*
(reduce
(fn [result seq]
(concat result (difference seq result)))
(map
prime-factorials
(range 1 21))))
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment