I've been playing around a bit at night using Scala to solve some problems at Project Euler. Many PE problems have to do with prime numbers so I needed to come up with a reusable sieve of eratosthenes to generate the primes. I started out using a fully functional sieve with no mutable state that produced an *infinite* lazy stream of prime numbers. It turned out to be slow and exhausted the heap for large primes. So I scrapped that and went with a more classic version using mutable arrays.
Here was my first attempt
And my code to
test the method (using the
specs testing framework):
And unexpectedly (to me at least) the test fails:
But when I replace arr.indices with a range object (0 until limit), it works as expected
To my eyes the code is equivalent, but obviously I'm missing something. What is it?
[ September 04, 2008: Message edited by: Garrett Rowe ]