Skip to content

Instantly share code, notes, and snippets.

@iwillspeak
Created September 4, 2020 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwillspeak/2d7698f780529f8cf41b25fdd380895f to your computer and use it in GitHub Desktop.
Save iwillspeak/2d7698f780529f8cf41b25fdd380895f to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Scheme
;; Implementation of the Sieve of Eratosthenes
;; https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
(define (eratosthenes n)
;; Mark multiples of the given prime in the vector
(define (mark-multiples p marked)
(define (mark-multiples-at p m marked)
(if (>= m (vector-length marked))
marked
(begin
(vector-set! marked m #t)
(mark-multiples-at p (+ m p) marked))))
(mark-multiples-at p (* p p) marked))
;; main prime sieve. Recursively marks multiples of each
;; prime and builds up a list of them as it goes.
(define (siv p marked primes)
(if (>= p (vector-length marked))
primes
;; If the item is marked, it is a multiple of some other prime.
(if (vector-ref marked p)
(siv (+ 1 p) marked primes)
(siv (+ 1 p) (mark-multiples p marked) (cons p primes)))))
(siv 2 (make-vector n #f) '()))
;; Display the first 10,000 primes
(display (eratosthenes 104730))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment