Sieve of Eratosthenes: Difference between revisions
Content deleted Content added
GordonBGood (talk | contribs) →{{header|JavaScript}}: cleaned and sped up code, added odds-only, "inifinite" dictionary and page segmented based versions... |
Emacs Lisp |
||
Line 1,332: | Line 1,332: | ||
</pre> |
</pre> |
||
=={{header|Emacs Lisp}}== |
|||
<lang lisp> |
|||
(defun sieve-set (limit) |
|||
(let ((xs (make-vector (1+ limit) 0))) |
|||
(loop for i from 2 to limit |
|||
when (zerop (aref xs i)) |
|||
collect i |
|||
and do (loop for m from (* i i) to limit by i |
|||
do (aset xs m 1))))) |
|||
</lang> |
|||
Straightforward implementation of [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Implementation sieve of Eratosthenes], 2 times faster: |
|||
<lang lisp> |
|||
(defun sieve (limit) |
|||
(let ((xs (vconcat [0 0] (number-sequence 2 limit)))) |
|||
(loop for i from 2 to (sqrt limit) |
|||
when (aref xs i) |
|||
do (loop for m from (* i i) to limit by i |
|||
do (aset xs m 0))) |
|||
(remove 0 xs))) |
|||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |