I am trying to solve this problem on Sphere Online Judge. I keep getting a timeout error. Any comments or suggestions are welcome and appreciated.package info.danforbes.sphere;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.nio.charset.MalformedInputException;import java.util.AbstractMap.SimpleEntry;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Map.Entry;public class PrimeNumberGenerator {  private static BufferedReader inStream;  private static BufferedWriter outStream;  private static List<Entry<Integer, Integer>> pairs = new ArrayList<Entry<Integer, Integer>>();  private static HashSet<Integer> primes = new HashSet<Integer>();  private static void getPairs(int numCases) throws MalformedInputException {    String line;    int spaceNdx;    int num1, num2;    try {      for (int pairNdx = 0; pairNdx < numCases; ++pairNdx) {        line = inStream.readLine();        spaceNdx = line.indexOf(' ');        num1 = Integer.parseInt(line.substring(0, spaceNdx));        num2 = Integer.parseInt(line.substring(spaceNdx + 1));        if (num1 < 1) throw new MalformedInputException(num1);        if (num1 > num2) throw new MalformedInputException(num2 - num1);        if (num2 > 1000000000) throw new MalformedInputException(num2);        if (num2 - num1 > 100000) throw new MalformedInputException(num2 - num1);        pairs.add(new SimpleEntry<Integer, Integer>(num1, num2));      }      inStream.close();    } catch (IOException e) {      System.out.println(IOException encounterd!);      e.printStackTrace();      System.exit(-1);    }  }  private static boolean isPrime(int num) {    boolean isPrime = true;    if (num < 2) isPrime = false;    else if (num > 3) {      if (num % 2 == 0 || num % 3 == 0) isPrime = false;      else if (!primes.contains(num)){        int sqrRoot = (int)Math.sqrt(num);        for (int factorNdx = 1; 6 * factorNdx - 1 <= sqrRoot; ++factorNdx) {          if (num % (6 * factorNdx + 1) == 0 || num % (6 * factorNdx - 1) == 0) {            isPrime = false;            break;          }        }      }    }    if (isPrime) primes.add(num);    return isPrime;  }  public static void main(String[] args) {    if (args.length == 0) {      inStream = new BufferedReader(new InputStreamReader(System.in));      outStream = new BufferedWriter(new OutputStreamWriter(System.out));    }    StringBuilder resultBuilder = new StringBuilder();    try {      if (inStream == null && outStream == null) {        inStream = new BufferedReader(new FileReader(args[0]));        outStream = new BufferedWriter(new FileWriter(args[1]));      }      int numCases = Integer.parseInt(inStream.readLine());      if (numCases > 10) throw new MalformedInputException(numCases);      getPairs(numCases);      int beg, end;      for (Entry<Integer, Integer> anEntry : pairs) {        beg = anEntry.getKey();        end = anEntry.getValue();        for (int rangeNdx = beg; rangeNdx <= end; ++rangeNdx) {          if (isPrime(rangeNdx)) resultBuilder.append(Integer.toString(rangeNdx) + \n);        }        resultBuilder.append(\n);      }      resultBuilder.delete(resultBuilder.length() - 2, resultBuilder.length());      outStream.write(resultBuilder.toString());      outStream.close();    } catch (IOException e) {      System.out.println(IOException encountered!);      e.printStackTrace();      System.exit(-1);    }  }}
Sphere Online Judge, Problem 2: Prime Number Generator
java;optimization;primes