Skip to content

Instantly share code, notes, and snippets.

@sebastianbenz
Created December 3, 2010 08:23
Show Gist options
  • Save sebastianbenz/726717 to your computer and use it in GitHub Desktop.
Save sebastianbenz/726717 to your computer and use it in GitHub Desktop.
Prime-factors kata in Clojure.
(ns prime-factors
(:use clojure.test)
(:use clojure.contrib.generic.math-functions))
(defn prime-factors
[n]
(loop [remainder n
result '()
factor 2]
(cond
(< remainder 2) result
(zero? (mod remainder factor)) (recur (/ remainder factor) (conj result factor) factor)
(< (sqrt remainder) factor) (conj result remainder)
:else (recur remainder result (inc factor)))))
(deftest prime-factors-test
(are [x y] (= x y)
'() (prime-factors 0)
'() (prime-factors 1)
'(2) (prime-factors 2)
'(2 2) (prime-factors 4)
'(3 2) (prime-factors 6)
'(5 3) (prime-factors 15)
'(5 2 2) (prime-factors 20)))
(run-all-tests #"[^(clojure)].*")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment