|
| 1 | +;; JAE - a temporary file used for testing, remove this down the road |
| 2 | + |
| 3 | + ;; erato.scm -- sieve of Eratosthenes |
| 4 | + |
| 5 | + ;; execution timings on a 1.9 GHz Intel Core2 + Win7-32 |
| 6 | + |
| 7 | + ;; all primes < 10^3 < 10^4 < 10^5 < 10^6 |
| 8 | + |
| 9 | + ;; interpreter |
| 10 | + |
| 11 | + ;; petite (chez scheme) 8.4 0 sec 0 sec 2 sec 2 min |
| 12 | + ;; PLT-r5rs (racket) 5.3.6 0 sec 0 sec 2 sec 4 min |
| 13 | + ;; pi (rhizome/pi) 0.57 0 sec 0 sec 2 sec 6 min |
| 14 | + ;; gsi (gambit) 4.7.0 0 sec 0 sec 4 sec 13 min |
| 15 | + ;; csi (chicken) 4.8.0.5 0 sec 0 sec 10 sec 18 min |
| 16 | + ;; scheme 48 1.9 0 sec 0 sec 10 sec n/a |
| 17 | + |
| 18 | + ;; notes |
| 19 | + ;; 1. most of these interpreters come with compilers. i have |
| 20 | + ;; not measured execution timings for compiled code. |
| 21 | + ;; 2. this is only a single application. execution timings |
| 22 | + ;; vary with application type and complexity of control |
| 23 | + ;; structures. |
| 24 | + ;; 3. rhizome/pi is my favorite interpreter. |
| 25 | +(define (append . lst) |
| 26 | + (define append-2 |
| 27 | + (lambda (inlist alist) |
| 28 | + (foldr (lambda (ap in) (cons ap in)) alist inlist))) |
| 29 | + (if (null? lst) |
| 30 | + lst |
| 31 | + (if (null? (cdr lst)) |
| 32 | + (car lst) |
| 33 | + (foldl (lambda (a b) (append-2 b a)) (car lst) (cdr lst))))) |
| 34 | + |
| 35 | + (define (primes<2n n) |
| 36 | + (erato 2 (* n 2) (list))) |
| 37 | + |
| 38 | + (define (erato n z primes) |
| 39 | + |
| 40 | + (define (sieve s) |
| 41 | + (define ok #f) |
| 42 | + (if (null? s) |
| 43 | + (set! ok #t) |
| 44 | + (if (< n (* (car s) (car s))) (set! ok #t))) ; prime! |
| 45 | + ;(if (or (null? s) ; prime |
| 46 | + ; (< n (* (car s) (car s)))) ; prime! |
| 47 | + (if ok |
| 48 | + (erato (+ n 1) z (append primes (list n))) |
| 49 | + (if (zero? (modulo n (car s))) ; composite |
| 50 | + (erato (+ n 1) z primes) |
| 51 | + (sieve (cdr s))))) |
| 52 | + |
| 53 | + (if (< n z) |
| 54 | + (sieve primes) |
| 55 | + primes)) |
| 56 | + |
| 57 | +((lambda () (primes<2n 1000) 1)) |
0 commit comments