Armando's suggested change worked fine for me.

(use '[clojure.contrib.lazy-seqs :only (primes)])

(defn prime-factors [n]
  (let [f (some #(if (= 0 (rem n %)) %) primes)]
    (println "n:" n ", f:" f)
    (if (= f n)
      (sorted-set f)
      (conj (prime-factors (/ n f)) f))))

user=> (prime-factors 600851475143)
n: 600851475143 , f: 71
n: 8462696833 , f: 839
n: 10086647 , f: 1471
n: 6857 , f: 6857
#{71 839 1471 6857}

On Jan 13, 10:01 am, Vitaly Peressada <vit...@ufairsoft.com> wrote:
> Armando, thanks for a plausible explanation. Here is what happened
> after I made the suggested change:
>
> user=> (prime-factors 600851475143)
> n: 600851475143 , f: 71
> n: 8462696833 , f: 839
> n: 10086647 , f: 1471
> n: 6857 , f: 6857
> #<CompilerException java.lang.ClassCastException: java.lang.Integer
> cannot be cast to clojure.lang.IFn (REPL:91)>
>
> I guess this something subtle with unordered vs. sorted-set as
> implicit factors accumulator. May be that is why the author of the
> solution had to use unordered set followed by (apply max ...).
>
> On Jan 13, 11:23 am, Armando Blancas <armando_blan...@yahoo.com>
> wrote:
>
>
>
>
>
>
>
> > A literal set is a unordered hash-set. To get the factors in order
> > change #{f} for (sorted-set f).
>
> > On Jan 13, 7:09 am, Vitaly Peressada <vit...@ufairsoft.com> wrote:
>
> > > The following solution by <b>mtgred</b> for <a href="http://clojure-
> > > euler.wikispaces.com/">Project Euler Clojure</a> problem 003 uses
> > > implicit recursion.
>
> > > <pre>
> > > (use '[clojure.contrib.lazy-seqs :only (primes)])
> > > (defn prime-factors [n]
> > >   (let [f (some #(if (= 0 (rem n %)) %) primes)]
> > >     (if (= f n) #{f} (conj (prime-factors (/ n f)) f))))
> > > (apply max (prime-factors 600851475143))
> > > </pre>
>
> > > Here is above with added println
>
> > > (defn prime-factors [n]
> > >   (let [f (some #(if (= 0 (rem n %)) %) primes)]
> > >     (println "n:" n ", f:" f)
> > >     (if (= f n)
> > >       #{f}
> > >       (conj (prime-factors (/ n f)) f))))
>
> > > Which produces
>
> > > n: 600851475143 , f: 71
> > > n: 8462696833 , f: 839
> > > n: 10086647 , f: 1471
> > > n: 6857 , f: 6857
> > > #{71 839 6857 1471}
>
> > > Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to