The two functions below should return true if the first argument is
divisible by any of the prime numbers (ascending) in the second
argument, and false otherwise. But the second function is much slower.
I haven't been able to figure out why. Can you?
Also, if there is a more idiomatic way, please share :)
(defn divisable? [n coll]
(let [count (count coll)
mx (int (Math/sqrt n))]
(loop [j 0]
(cond (= j count) true
(> (nth coll j) mx) true
(factor? n (nth coll j)) false
:else (recur (inc j))))))
(defn divisable-slow? [n coll]
(let [limit (int (Math/sqrt n))]
(not-any? #(factor? n %) (filter #(<= % limit) coll))))
--
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