This is just my copy of something I pulled together from other sources
while working of Project Euler problems and perhaps refined a little:

(defn prime?
 [n]
 (cond
  (or (= n 2) (= n 3)) true
  (even? n) false
  :else (let [sqrt-n (Math/sqrt n)]
         (loop [i 3]
          (cond
           (divisible? n i) false
           (< sqrt-n i)     true
           :else            (recur (+ i 2)))))))


I settled on this one because it seems to be the fastest.

Of course, there is a library implementation somewhere.

What I am curious about is figuring out a way to retain primes in a
useful way. In this example, we start with 3 and just keep adding 2,
but if you are doing something like (filter prime? (range 2 10000)),
wouldn't there be some ideal way to memoize the prime values you've
discovered so far and use them as the values to check against? Perhaps
this just leads to more overhead than it is worth, but I haven't
explored it yet, I've been too busy with other stuff lately.

-- 
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