Last active
August 29, 2015 14:00
-
-
Save tim-brown/87b05c16f41aa86a1421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define lucid? | |
(let ((lucid-bytes-sieve | |
(delay | |
(define sieve-bytes (make-bytes lucid-sieve-size 1)) | |
(bytes-set! sieve-bytes 0 0) ; not a lucid number | |
(define (sieve-pass L) | |
(let loop ((idx (add1 L)) (skip (sub1 L))) | |
(cond | |
[(= idx lucid-sieve-size) | |
(for/first ((rv (in-range (add1 L) lucid-sieve-size)) | |
#:unless (zero? (bytes-ref sieve-bytes rv))) rv)] | |
[(zero? (bytes-ref sieve-bytes idx)) | |
(loop (add1 idx) skip)] | |
[(= skip 0) | |
(bytes-set! sieve-bytes idx 0) | |
(loop (add1 idx) (sub1 L))] | |
[else (loop (add1 idx) (sub1 skip))]))) | |
(let loop ((l 2)) | |
(when l (loop (sieve-pass l)))) | |
sieve-bytes))) | |
(λ (n) (= 1 (bytes-ref (force lucid-bytes-sieve) n))))) | |
(require data/bit-vector) | |
(define (make-lucid-bits-sieve) | |
(define sieve-bytes (make-bit-vector lucid-sieve-size #t)) | |
(bit-vector-set! sieve-bytes 0 #f) ; not a lucid number | |
(define (sieve-pass L) | |
(let loop ((idx (add1 L)) (skip (sub1 L))) | |
(cond | |
[(= idx lucid-sieve-size) | |
(for/first ((rv (in-range (add1 L) lucid-sieve-size)) | |
#:when (bit-vector-ref sieve-bytes rv)) | |
rv)] | |
[(not (bit-vector-ref sieve-bytes idx)) | |
(loop (add1 idx) skip)] | |
[(= skip 0) | |
(bit-vector-set! sieve-bytes idx #f) | |
(loop (add1 idx) (sub1 L))] | |
[else (loop (add1 idx) (sub1 skip))]))) | |
(let loop ((l 2)) (when l (loop (sieve-pass l)))) | |
sieve-bytes) | |
(define lucid-bits-sieve (time (make-lucid-bits-sieve))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment