nsieve.scm 922 B

123456789101112131415161718192021222324252627
  1. (define (nsieve (m :: int) (is-prime :: boolean[])) :: int
  2. (do ((i :: int 2 (+ i 1)))
  3. ((> i m) #!void)
  4. (set! (is-prime i) #t))
  5. (define count :: int 0)
  6. (do ((i :: int 2 (+ i 1)))
  7. ((> i m) count)
  8. (cond ((is-prime i)
  9. (do ((k :: int (+ i i) (+ k i)))
  10. ((> k m) count)
  11. (set! (is-prime k) #f))
  12. (set! count (+ count 1))))))
  13. (define (test (n :: int))
  14. (if (< n 2) (set! n 2))
  15. (define m :: int (* (bitwise-arithmetic-shift 1 n) 10000))
  16. (define flags (make boolean[] length: (+ m 1)))
  17. (format #t "Primes up to ~8d~9d~%" m (nsieve m flags))
  18. (set! m (* (bitwise-arithmetic-shift 1 (- n 1)) 10000))
  19. (format #t "Primes up to ~8d~9d~%" m (nsieve m flags))
  20. (set! m (* (bitwise-arithmetic-shift 1 (- n 2)) 10000))
  21. (format #t "Primes up to ~8d~9d~%" m (nsieve m flags)))
  22. (define args (cdr (command-line)))
  23. (test (if (null? args) 0
  24. (java.lang.Integer:parseInt (args 0))))