I’m trying to implement sieve of Eratosthenes in Clojure. One approach I would like to test is this:
- Get range (2 3 4 5 6 … N)
- For 2 <=
i<= N- Pass my range through
filterthat removes multiplies ofi - For
i+1th iteration, use result of the previous filtering
- Pass my range through
I know I could do it with loop/recur, but this is causing stack overflow errors (for some reason tail call optimization is not applied).
How can I do it iteratively? I mean invoking N calls to the same routine, passing result of ith iteration to i+1th.
This one seems to work just fine. Tested it with (sieve 2 1000000) and returns within seconds without blowing.