Skip to content

Instantly share code, notes, and snippets.

@zipcode
Created July 29, 2016 22:53
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 zipcode/c6bfe71726bedda4431f8cb8e95470bd to your computer and use it in GitHub Desktop.
Save zipcode/c6bfe71726bedda4431f8cb8e95470bd to your computer and use it in GitHub Desktop.
corecursive infinite list of primes in clojure
(defn divides?
[x y]
(and (= 0 (mod x y)) (not (= x y))))
(def primes
(lazy-cat
[2 3]
(filter
(fn [x]
(not (some
#(divides? x %)
(take-while
#(<= % (Math/ceil (Math/sqrt x)))
primes))))
(iterate inc 5))))
(defn prime? [x]
(not (some
#(divides? x %)
(take-while
#(<= % (Math/ceil (Math/sqrt x)))
primes))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment