Skip to content

Instantly share code, notes, and snippets.

@cky
Last active December 11, 2015 02:48
Show Gist options
  • Save cky/4532978 to your computer and use it in GitHub Desktop.
Save cky/4532978 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
#lang racket
(define (primes-less-than n)
(define size (sub1 (quotient n 2)))
(define table (build-vector size (lambda (i) (+ i i 3))))
(for ((x (in-vector table 0 (integer-sqrt size)))
#:when x
(y (in-range (quotient (- (* x x) 3) 2) size x)))
(vector-set! table y #f))
(cons 2 (filter values (vector->list table))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment