Source

euler / src / eratosthenes.clj

;; Sieve of Eratosthenes
;; http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

;(defn sieve [coll & curr] 
;	(if (nil? curr)
;		(sieve coll 2)
;		(cons curr (sieve (remove) ()))
;(defn sieve [coll] 
;	(let [divisible? (fn [x] (= 0 (mod x (first coll))))] 
;	(cons (first coll) (sieve (remove divisible? coll) ))))

;(defn sieve [coll] 
;	(loop [remaining]
;		(if (empty? remaining)
;			nil
;			
;			(let [divisible? (fn [x] (= 0 (mod x (first coll))))] 
;			(cons (first coll) (recur(remove divisible? coll)))))


(defn sieve [coll] 
	(loop [result  ()
				 remaining (seq coll)]
		(let [divisible? (fn [x] (= 0 (mod x (first remaining))))] 
		(if (empty? remaining)
			result
			(recur (cons (first remaining) result) (remove divisible? (rest remaining)))))))

(println (count (sieve (range 2 10000))))
;; FIXME this blows up for big numbers