didn't need the assoc in my previous try
(defn of [n]
(letfn [(f [res k]
(if (= 0 (rem (:n res) k))
{:n (/ (:n res) k) :fs (conj (:fs res) k)}
res))]
(:fs (reduce f {:n n :fs []} (range 2 n)))))
On Fri, Jun 11, 2010 at 3:15 PM, russellc <[email protected]>wrote:
> Not sure it's better than Uncle Bobs version but it seems a little
> more idiomatic?
>
> (defn of [n]
> (letfn [(f [res k]
> (if (= 0 (rem (:n res) k))
> (assoc (assoc res :n (quot (:n res) k)) :fs (conj (:fs
> res) k))
> res))]
> (:fs (reduce f {:n n :fs []} (range 2 n)))))
>
> Uncle Bob version below (http://blog.objectmentor.com/articles/
> 2010/05/15/clojure-prime-factors)
>
> (defn of
> ([n]
> (of [] n 2))
> ([factors n candidate]
> (cond
> (= n 1) factors
> (= 0 (rem n candidate)) (recur (conj factors candidate) (quot n
> candidate) candidate)
> (> candidate (Math/sqrt n)) (conj factors n)
> :else (recur factors n (inc candidate))
> )
> )
> )
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]<clojure%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en