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