s c h e m a t i c s : c o o k b o o k

/ Cookbook.SieveOfEratosthenes

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Block-based "Sieve of Eratosthenes"

Problem

The "Sieve of Eratosthenes" is simple algorithm for finding primes. It starts with a big table of numbers and "crosses out" all multiples of 2, 3, 5, 7...

Solution

;; return a a byte vector representation of the primes between 3 and
;; `size'
(define (make-base-block size)
  (let ((block (make-byte-vector size 1))
        (limit (inexact->exact (truncate (/ (sqrt (* size 2)) 2)))))
    (do ((i 0 (+ 1 i)))
        ((>= i limit) block)
      (if (= (byte-vector-ref block i) 1)
          (let ((prime (+ (* i 2) 3)))
            (do ((k (+ i prime) (+ k prime)))
                ((>= k size) #t)
              (byte-vector-set! block k 0)))))))

;; Given a block of primes in base-block (which must cover the primes
;; up to `(sqrt (+ base (byte-vector-length block)))', set all
;; locations in `block' to 1 that are prime and all others to 0.
(define (sieve-block! base-block base block)
  (byte-vector-fill! block 1)
  (let* ((size (byte-vector-length block))
         (limit (quotient (truncate (sqrt (+ base (* size 2)))) 2)))
    (do ((i 0 (+ i 1)))
        ((>= i limit) block)
      (if (= (byte-vector-ref base-block i) 1)
          (let* ((prime (+ (* i 2) 3))
                 (multiple (let loop ((multiple (* (quotient base prime) prime)))
                             (if (or (< multiple base)
                                     (= (remainder multiple 2) 0))
                                 (loop (+ multiple prime))
                                 multiple))))
            (do ((k (quotient (- multiple base) 2) (+ k prime)))
                ((>= k size) #t)
              (byte-vector-set! block k 0)))))))

Discussion

The number of iterations in procedure make-base-block can be reduced by replacing the line

(do ((k (+ i prime) (+ k prime)))

by

(do ((k (/ (- (* prime prime) 3) 2) (+ k prime)))

Because all multiples of the prime smaller than (* prime prime) have already been erased. JosKoot - 24 Nov 2006


Comments about this recipe

Contributors

-- AndreasRottmann - 08 Jun 2005

CookbookForm
TopicType: Recipe
ParentTopic: NumberRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest