FileDocCategorySizeDatePackage
PrimeWriter.javaAPI DocExample1272Sun Mar 28 07:20:22 BST 1999None

PrimeWriter

public class PrimeWriter extends Thread

Fields Summary
DataOutputStream
theOutput
int
numPrimes
Constructors Summary
public PrimeWriter(OutputStream out, int numPrimes)

    theOutput = new DataOutputStream(out);
    this.numPrimes = numPrimes;
  
public PrimeWriter(OutputStream out)

    this(out, Integer.MAX_VALUE);
  
Methods Summary
public static intnextPrime(int n)

      
    nextPossibility: for (int possiblePrime = n+1; ; possiblePrime++) {
      // watch for overflow
      if (possiblePrime < 0) throw new IOException("Arithmetic Overflow");
      for (int factor = 2; factor <= Math.sqrt(possiblePrime); factor++) {
        if (possiblePrime % factor == 0) continue nextPossibility;
      }
      return possiblePrime;
    }

  
public voidrun()


    int lastPrime = 1;
    for (int i = 0; i < numPrimes; i++) {
      try {
        lastPrime = nextPrime(lastPrime);
        theOutput.writeInt(lastPrime);
      }
      catch (IOException e) {
        System.err.println(e);
      }
      yield();
    }