Scheme

Scheme is Lisp with different functions and different special forms. Arrays become vectors, variables must be defined, #f replaces nil in conditionals, a more complicated do construct replaces dotimes and the conditional constructs are different.

The following code runs under MIT-GNU Scheme. To run the code, click the link below and save the "SieveE.scm" file in your Scheme startup directory. Then type the following line to run the code.

(load "SieveE")

The output runs off the top of the window and the window is not scrollable, but I'll leave this problem to you.

; create an array for the sieve (define candidates (make-vector 1000)) ; Set all array elements to true. Indices represent the integers (define i 0) (define j 0) (do ((i 0 (+ i 1))) ((= i 1000) 0) (vector-set! candidates i #t) ) ; zero and one are not prime, turn them off (vector-set! candidates 0 #f) (vector-set! candidates 1 #f) ; 31 is good enough, 32 squared is > 1000 (do ((i 0 (+ i 1))) ((= i 32) 0) ; Find first ON element -- must be prime (if (vector-ref candidates i) ; Turn off all multiples of p, starting with 2*p (do ((j 2 (+ j 1))) ((>= (* i j) 1000) 0) (vector-set! candidates (* i j) #f) ) ) ) ; print all primes (do ((i 0 (+ i 1))) ((= i 1000) 0) (if (vector-ref candidates i) (begin (display i) (display " is prime") (newline) ) ) )

Click Here for the actual code.