Hi,

I created an infinite stream of primes (using this interesting article:
http://matthias.benkard.de/journal/116)

I tried to create a prime-pi function:

  (define (prime-pi n)
    (let loop-count ([candidate-stream prime-stream])
      (if (> (stream-first candidate-stream) n)
          0
          (+ (loop-count (stream-rest candidate-stream)) 1))))

(prime-pi 500000) grinds and runs out of memory

But this little cludge makes it run just fine:

  (define (prime-pi n)
    (stream-ref prime-stream (inexact->exact (* 0.1 n)))  ; cludge added to
get the stream to pre-create enough primes
    (let loop-count ([candidate-stream prime-stream])
      (if (> (stream-first candidate-stream) n)
          0
          (+ (loop-count (stream-rest candidate-stream)) 1))))

What am I not understanding?

Thanks,
-Joe
____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Reply via email to