I tried implementing the sieve of Eratosthenes in Clojure. The approach is to loop while (A) keeping my current list of numbers which have not yet been eliminated, and (B) keeping track of the number I'm "on" (checking for multiples thereof).
Here's what I came up with (in a foo.clj file): ~~~clojure (declare remove-multiples-of) (declare next-one-after) (defn main [max-num] (let [starting-nums (range 2 max-num)] (loop [new-nums (remove-multiples-of 2 starting-nums) new-num (next-one-after 2 new-nums)] (if (or (nil? new-num) (> new-num (* 0.5 max-num))) new-nums (recur (remove-multiples-of new-num new-nums) (next-one-after new-num new-nums)))))) (defn remove-multiples-of "Removes all multiples of n (but not $n * 1$) from xs." [n xs] (remove (fn [x] (and (> x n) (zero? (rem x n)))) xs)) (defn next-one-after "It's assumed that xs is an ordered sequential collection of positive integers, and that n is among xs. Returns the element right after n, unless n is the last element --- if that's the case, just returns nil." [n xs] (second (drop-while (fn [x] (not= x n)) xs))) ;;--------------------------- (println (last (main 5000))) ~~~ Running that (via `lein exec foo.clj`), it appears to work. However, if I change that 5000 to 50000, I get a long disapproving-looking stack trace starting with java.lang.StackOverflowError. Why is it overflowing? Thanks! -- -- 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.