Created
August 19, 2023 02:01
-
-
Save magnomp/4ada66b72a27194cb4348e61fff037d7 to your computer and use it in GitHub Desktop.
Prime factorization in Clojure
This file contains hidden or 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 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