views:

189

answers:

1
+1  A: 

So, I extracted out the following method:

    private static void printPrimes(int lower_bound, int upper_bound) {
 ArrayList<Integer> primesList = GeneratePrimesSieveOfEratosthenes(upper_bound);

 for (int i = 0; i < primesList.size(); i++) {
  if (primesList.get(i) <= lower_bound)
   System.out.println(primesList.get(i));
 }
}

and changed the main() method to just call that with a couple of arbitrary arguments (10 and 100), because I didn't want to mess around with the console and the debugger at the same time. I then (I'm using Eclipse) put ordinary breakpoints at the beginning and end lines of ApproximateNthPrime(), SieveOfEratosthenes() and GeneratePrimesSieveOfEratosthenes() to make sure they were being called. (By the way, Java convention, unlike C#, is for method names to start with a lower-case letter.)

All that was without bothering to understand the code. :) However, after the first run-through, it was pretty clear that the problem is that the BitSet produced by SieveOfEratosthenes() is always empty (or rather, always entirely false). I haven't used the NetBeans debugger, but I suspect the "Local Variables" tab is your friend here.

I'm not going to do your homework for you. :) But the idea of the Sieve of Eratosthenes is to skip the prime numbers and only eliminate the non-primes. Examine your SieveOfEratosthenes() method and ask yourself: when will it skip a number?

David Moles