Skip to content

Instantly share code, notes, and snippets.

@magnomp
Created August 19, 2023 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magnomp/4ada66b72a27194cb4348e61fff037d7 to your computer and use it in GitHub Desktop.
Save magnomp/4ada66b72a27194cb4348e61fff037d7 to your computer and use it in GitHub Desktop.
Prime factorization in Clojure
(ns hello)
(defn is-divisible-by
[number divisor]
(= (rem number divisor) 0))
(defn factor-by
[number factor acc]
(loop [n number acc1 acc]
(if (is-divisible-by n factor)
(recur (quot n factor) (conj acc1 factor))
[n acc1]
)
))
(defn prime-factors
[number]
(loop [n number acc [] factor 2]
(if (= n 1)
acc
(let [[n acc] (factor-by n factor acc)]
(recur n acc (+ 1 factor)))))
)
(print (prime-factors 200277))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment