Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Written a Clojure lazy-seq function to generate n prime numbers. It generates .5 million prime numbers in 20 secs
;; Clojure lazy-seq function to generate n prime numbers.
;; It generates .5 million prime numbers in 20 secs using
;; the naive non-sieve algo
;; Author: Abhishek Gupta (@abhilater)
(defn n-primes
[]
(filter (fn [num]
(loop [end (int (Math/sqrt num)), div 2, re (rem num div)]
(cond
(< num 2) false
(= num 2) true
(= re 0) false
(> div end) true
:else (recur end (inc div) (rem num div)))
)) (range)))
;;; (take 500000 (n-primes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment