Skip to content

Instantly share code, notes, and snippets.

@benatkin
Created May 29, 2009 23:36
Show Gist options
  • Select an option

  • Save benatkin/120282 to your computer and use it in GitHub Desktop.

Select an option

Save benatkin/120282 to your computer and use it in GitHub Desktop.
(defn factor [n]
(let [max (int (/ n 2))]
(loop [x 2]
(if (zero? (rem n x))
(conj (factor (/ n x)) x)
(if (< x max)
(recur (+ x 1))
(if (= n 1) [] [n]))))))
(println (factor 5)) ; 5
(println (factor 13)) ; 13
(println (factor 32)) ; [2 2 2 2 2]
(println (factor 269)) ; [269]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment