Skip to content

Instantly share code, notes, and snippets.

@mharju
Created July 26, 2012 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharju/3181751 to your computer and use it in GitHub Desktop.
Save mharju/3181751 to your computer and use it in GitHub Desktop.
Prime number sieve
(defn multiple-of [number divisor] (and (not (= number divisor))
(= (mod number divisor) 0)))
(defn not-multiples-or-1 [numbers divisor]
(filter (fn [n]
(and
(not (= n 1))
(not (multiple-of n divisor)))) numbers))
(defn primes-in-decreasing-seq [n] (reduce not-multiples-or-1 n (range 2 (Math/sqrt (first n)))))
(defn primes-in-increasing-seq [n] (reduce not-multiples-or-1 n (range 2 (Math/sqrt (last n)))))
(defn primes-in-seq [n] (reduce not-multiples-or-1 n (range 2 (Math/sqrt (apply max n)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment