Skip to content

Instantly share code, notes, and snippets.

@pbadenski
Created May 7, 2010 08:58
Show Gist options
  • Save pbadenski/393212 to your computer and use it in GitHub Desktop.
Save pbadenski/393212 to your computer and use it in GitHub Desktop.
(with-test
(defn prime-factors [n]
((fn this [n_ last-divisor]
(if (= n_ 1)
[]
(let [
divisor
(or
(some
#(when (zero? (mod n_ %)) %)
(range last-divisor (. Math sqrt n)))
n_)]
(cons
divisor
(this (/ n_ divisor) last-divisor)))))
n 2))
(are (= (prime-factors _1) _2)
1 []
2 [2]
3 [3]
4 [2 2]
5 [5]
6 [2 3]
7 [7]
8 [2 2 2]
(dec (** 2 17)) [(dec (** 2 17))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment