Sieve of Eratosthenes: Difference between revisions
Content deleted Content added
Changed Float80 to Double to increases compatibility with Apple Silicon |
Emacs Lisp: Use cl-lib |
||
Line 6,215: | Line 6,215: | ||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
{{libheader|cl-lib}} |
|||
<lang lisp> |
|||
(defun sieve-set (limit) |
<lang lisp>(defun sieve-set (limit) |
||
(let ((xs (make-vector (1+ limit) 0))) |
(let ((xs (make-vector (1+ limit) 0))) |
||
(loop for i from 2 to limit |
(cl-loop for i from 2 to limit |
||
when (zerop (aref xs i)) |
when (zerop (aref xs i)) |
||
collect i |
collect i |
||
and do (loop for m from (* i i) to limit by i |
and do (cl-loop for m from (* i i) to limit by i |
||
do (aset xs m 1))))) |
do (aset xs m 1)))))</lang> |
||
</lang> |
|||
Straightforward implementation of [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Implementation sieve of Eratosthenes], 2 times faster: |
Straightforward implementation of [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Implementation sieve of Eratosthenes], 2 times faster: |
||
<lang lisp> |
<lang lisp>(defun sieve (limit) |
||
(defun sieve (limit) |
|||
(let ((xs (vconcat [0 0] (number-sequence 2 limit)))) |
(let ((xs (vconcat [0 0] (number-sequence 2 limit)))) |
||
(loop for i from 2 to (sqrt limit) |
(cl-loop for i from 2 to (sqrt limit) |
||
when (aref xs i) |
when (aref xs i) |
||
do (loop for m from (* i i) to limit by i |
do (cl-loop for m from (* i i) to limit by i |
||
do (aset xs m 0))) |
do (aset xs m 0))) |
||
(remove 0 xs))) |
(remove 0 xs)))</lang> |
||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |