(conj) will add items to different places in the collection, depending
on the type of the collection. For a set, this can be at either the
end OR the beginning.
For this problem, the output is built by:
(conj (conj (conj #{6857} 1471) 839) 71)
which is equivalent to:
(-> #{6857} (conj 1471) (conj 839) (conj 71))
The innermost/first conj, (conj #{6857} 1471), sticks 1471 at the end
and produces #{6857 1471}. The next conj sticks 839 at the beginning
and produces #{839 6857 1471}. The final conj sticks 71 at the
beginning and produces the final result of #{71 839 6857 1471}.
When ordering matters, I usually use either (cons), which will always
add to the beginning regardless of the collection type, or use (conj)
with a list (will always add to the beginning) or vector (will always
add to the end).
user=> (-> '(6857) (conj 1471) (conj 839) (conj 71))
(71 839 1471 6857)
user=> (-> [6857] (conj 1471) (conj 839) (conj 71))
[6857 1471 839 71]
On Jan 13, 8:09 am, Vitaly Peressada <[email protected]> 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 [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