Re: Can anyone create a simpler version of prime factors in Clojure?

2010-06-16 Thread Daniel
Here's my awful terrible code which is a direct translation from a java version I wrote and needs a fast functional sieve: I prefer Cristophe Grande's. http://clj-me.cgrand.net/index.php?s=Everybody%20loves%20the%20Sieve%20of%20Eratosthenes The one in contrib is pretty good as well. (letfn [(n

Re: Can anyone create a simpler version of prime factors in Clojure?

2010-06-13 Thread David Cabana
I looked through some of my Project Euler solutions and found this version. It is essentially the same as Uncle Bob's, but to my eye it is a bit easier to read. (defn least-nontrivial-divisor [n];; integer n > 1 (loop [k 2] (cond (zero? (rem n k)) k ;; k divides n,

Re: Can anyone create a simpler version of prime factors in Clojure?

2010-06-12 Thread Russell Christopher
I focused more on idiom than correctness (obviously). Took a risk with not much effort except to see if I could do any better with a toy problem. The answer was no, Uncle Bob's is "idiomatic" modulo some minor things. On Sat, Jun 12, 2010 at 12:59 PM, Steve Purcell wrote: > On 12 Jun 2010, at 16

Re: Can anyone create a simpler version of prime factors in Clojure?

2010-06-12 Thread Steve Purcell
On 12 Jun 2010, at 16:18, Russell Christopher wrote: > You're right. Hope I haven't offended with the fail, I thought I had tested > it - by iterating over a range and comparing it to Uncle Bob's but obviously > I didn't do that right and then realized that factorization is likely not > O(n) an

Re: Can anyone create a simpler version of prime factors in Clojure?

2010-06-12 Thread Russell Christopher
You're right. Hope I haven't offended with the fail, I thought I had tested it - by iterating over a range and comparing it to Uncle Bob's but obviously I didn't do that right and then realized that factorization is likely not O(n) anyway. I'll probably take more time next time. Regards, Russell

Re: Can anyone create a simpler version of prime factors in Clojure?

2010-06-12 Thread Steve Purcell
On 11 Jun 2010, at 20:35, Russell Christopher wrote: > 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

Re: Can anyone create a simpler version of prime factors in Clojure?

2010-06-11 Thread Russell Christopher
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 wrote:

Can anyone create a simpler version of prime factors in Clojure?

2010-06-11 Thread russellc
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 []} (r