Skip to content

Instantly share code, notes, and snippets.

@madosuki
Created April 4, 2020 05:25
Show Gist options
  • Save madosuki/0edf91458e6a16f89244b2aee040ae40 to your computer and use it in GitHub Desktop.
Save madosuki/0edf91458e6a16f89244b2aee040ae40 to your computer and use it in GitHub Desktop.
(defun sieve-of-eratosthenes (&optional (target-number 128))
(let ((search-list (loop for i from 2 to target-number
if (/= (mod i 2) 0)
collect i))
(result (list 2))
(range-max (sqrt target-number)))
(labels ((calculator ()
(if (or (null (car search-list)) (>= (sqrt (car search-list)) range-max))
(nreverse result)
(progn
(push (car search-list) result)
(setq search-list (remove-if #'(lambda (n) (= (mod n (car result)) 0)) search-list))
(calculator)))))
(calculator))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment