Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Created August 12, 2021 23:00
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 mdwhatcott/4e13110f14f84dc0cb8b1c7820cbf1eb to your computer and use it in GitHub Desktop.
Save mdwhatcott/4e13110f14f84dc0cb8b1c7820cbf1eb to your computer and use it in GitHub Desktop.
prime-factors.clj
(ns factors.core-spec
(:require [speclj.core :refer :all]
[factors.core :refer :all]))
(defn factors-of [n]
(loop [n n, d 2, fs []]
(cond (= n 1) fs
(zero? (mod n d)) (recur (/ n d) d (conj fs d))
:else (recur n (inc d) fs))))
(describe "Prime Factors Kata"
(it "computes prime factors"
(->> (factors-of 1) (should= []))
(->> (factors-of 2) (should= [2]))
(->> (factors-of 3) (should= [3]))
(->> (factors-of 4) (should= [2 2]))
(->> (factors-of 5) (should= [5]))
(->> (factors-of 6) (should= [2 3]))
(->> (factors-of 7) (should= [7]))
(->> (factors-of 8) (should= [2 2 2]))
(->> (factors-of 9) (should= [3 3]))
(->> (factors-of 10) (should= [2 5]))
(->> (factors-of (* 2 3 5 7 11 13 17))
(should= [2 3 5 7 11 13 17]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment