fork(1) download
  1. ; numbers with 3 divisors
  2.  
  3. (define (isqrt n)
  4. (if (not (and (positive? n) (integer? n)))
  5. (error 'isqrt "must be positive integer")
  6. (let loop ((x n))
  7. (let ((y (quotient (+ x (quotient n x)) 2)))
  8. (if (< y x) (loop y) x)))))
  9.  
  10. (define (primes n) ; list of primes not exceeding n
  11. (let* ((len (quotient (- n 1) 2)) (bits (make-vector len #t)))
  12. (let loop ((i 0) (p 3) (ps (list 2)))
  13. (cond ((< n (* p p))
  14. (do ((i i (+ i 1)) (p p (+ p 2))
  15. (ps ps (if (vector-ref bits i) (cons p ps) ps)))
  16. ((= i len) (reverse ps))))
  17. ((vector-ref bits i)
  18. (do ((j (+ (* 2 i i) (* 6 i) 3) (+ j p)))
  19. ((<= len j) (loop (+ i 1) (+ p 2) (cons p ps)))
  20. (vector-set! bits j #f)))
  21. (else (loop (+ i 1) (+ p 2) ps))))))
  22.  
  23. (define (f n) (length (primes (isqrt n))))
  24.  
  25. (display (f 1000000000)) (newline)
Success #stdin #stdout 0.02s 8600KB
stdin
Standard input is empty
stdout
3401