Input: an integer n > 1
Let A be an array of bool values, indexed by integers 2 to n,
initially all set to true.
for i = 2, 3, 4, ..., while i^2 ≤ n:
if A[i] is true:
for j = i^2, i^2 + i, i^2 + 2i, ..., while j ≤ n:
A[j] = false
Now all i such that A[i] is true are prime.Since the keys are consecutive integers, you might consider a vector
of booleans:
(def A (vec (repeat (inc n) true)))
You can then use assoc to set any particular index to false:
(assoc A 5 false)
Differences using a vector instead of a hash-map: will stay in numeric
order, doesn't allow arbitrary dissoc, nth and assoc may be faster,
use less memory, (vector-of :bool) will definitely use less memory,
and probably others. Depending on your goals, a vector may or may not
be preferable to a hash-map.
--Chouser
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en