Question

I'm both learning to use the JPDA on Netbeans and solving the Prime Generator problem of Sphere's Online Judge.

I've been reading this tutorial on netbeans.org about he JPDA, but haven't found it of much help.

This code, which is based on a Sieve of Eratostenes implementation provided by starblue here, is running like this:

2
1 10
//here the primes between 1 and 10 should print 
3 5
//here the primes between 3 and 5 should print 




package sphere;

/**
 *
 * @author Administrator
 */
//import java.util.ArrayList;
import java.util.BitSet;
import java.lang.Math.*;
import java.util.ArrayList;

public class Main
{

  public static int ApproximateNthPrime(int nn)
{
    double n = (double)nn;
    double p;
    if (nn >= 7022)
    {
        p = n * Math.log(n) + n * (Math.log(Math.log(n)) - 0.9385);
    }
    else if (nn >= 6)
    {
        p = n * Math.log(n) + n * Math.log(Math.log(n));
    }
    else if (nn > 0)
    {
        p = new int[] { 2, 3, 5, 7, 11 }[nn - 1];
    }
    else
    {
        p = 0;
    }
    return (int)p;
}

// Find all primes up to and including the limit
public static BitSet SieveOfEratosthenes(int limit)
{
    final BitSet primes = new BitSet();
    primes.set(0,false); 
    primes.set(1,false); 
    primes.set(2,limit,true); 

    for (int i =0; i*i<limit;i++)
    {
        if (primes.get(i))
        {
            for (int j=i*1; j<limit;j+=1)
            {
                primes.clear(j);// hace que el indice j sea false (no primo)
            }

        }

    }
    return primes;
}

public static ArrayList<Integer> GeneratePrimesSieveOfEratosthenes(int n)
{
    int limit = ApproximateNthPrime(n);
    BitSet bits = SieveOfEratosthenes(limit);
    ArrayList <Integer> primes = new ArrayList<Integer>();
    for (int i = 0, found = 0; i < limit && found < n; i++)
    {
        if (bits.get(i))
        {
            primes.add(i);
            found++;
        }
    }
    return primes;
}





  public static void main (String[] args) throws java.lang.Exception
  {
     java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
     String s;

     s= r.readLine();

     int test_cases = Integer.parseInt(s);


     int case_counter =0;

     while (case_counter<test_cases) {

        // System.out.println(s);
         s = r.readLine();

         String [] splitted = s.split(" ");

         int lower_bound = Integer.parseInt(splitted[0]);
         int upper_bound = Integer.parseInt(splitted[1]);



        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));
         }


         case_counter++;

         System.out.println(" "); // space that separates test cases

     }
  }
}

I know that the ArrayList primesList isn't getting initialized and I'm suspicious of this bit of code, cause honestly, I don't quite understand it:

if (primes.get(i))
        {
            for (int j=i*1; j<limit;j+=1)
            {
                primes.clear(j);
            }

        }

It occurred to me to use a conditional breakpoint here with the condition of:

primes.get(j)==false

But I'm not sure if I'm able to get meaningful info this way. These are the screens I'm getting:

alt text http://img525.imageshack.us/img525/6238/breakpoints.jpg

alt text http://img98.imageshack.us/img98/5262/watchesz.jpg

I don't know how to get useful info out of this.

My questions are:

a)I want to watch the primes BitSet as its going through this loop.

How do I do that?

b) What's exactly wrong with this code? How did you spot it using the debugger?

Please, mention the step-by-step process.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top