Created
December 3, 2010 08:23
-
-
Save sebastianbenz/726717 to your computer and use it in GitHub Desktop.
Prime-factors kata in Clojure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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