Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Created February 28, 2018 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinmeiners/b42f717c5a315a68c6ca82c11eeb12c4 to your computer and use it in GitHub Desktop.
Save justinmeiners/b42f717c5a315a68c6ca82c11eeb12c4 to your computer and use it in GitHub Desktop.
(define (prime-sieve count)
(let ((marked (make-vector count #f)))
(define (build-list index)
(if (>= index count)
'()
(if (vector-ref marked index)
(build-list (+ index 1))
(cons (+ index 1) (build-list (+ index 1))))))
(define (next-prime index)
(if (>= index count)
marked
(begin
(if (vector-ref marked index)
'done
(mark (+ index 1) (+ index index 1)))
(next-prime (+ index 1)))))
(define (mark stride index)
(if (>= index count)
'ok
(begin
(vector-set! marked index #t)
(mark stride (+ index stride)))))
(begin (next-prime 1) (build-list 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment