Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
7.3 Persistent Servlet State and Auto Reloading Pages
161
Listing 7.3 PrimeList.java (continued)
public synchronized Vector getPrimes() {
if (isDone())
return(primesFound);
else
return((Vector)primesFound.clone());
}
public int numDigits() {
return(numDigits);
}
public int numPrimes() {
return(numPrimes);
}
public synchronized int numCalculatedPrimes() {
return(primesFound.size());
}
}
Listing 7.4 Primes.java
package coreservlets;
import java.math.BigInteger;
/** A few utilities to generate a large random BigInteger,
* and find the next prime number above a given BigInteger.
*/
public class Primes {
// Note that BigInteger.ZERO was new in JDK 1.2, and 1.1
// code is being used to support the most servlet engines.
private static final BigInteger ZERO = new BigInteger("0");
private static final BigInteger ONE = new BigInteger("1");
private static final BigInteger TWO = new BigInteger("2");
// Likelihood of false prime is less than 1/2^ERR_VAL
// Assumedly BigInteger uses the Miller Rabin test or
// equivalent, and thus is NOT fooled by Carmichael numbers.
// See section 33.8 of Cormen et al's Introduction to
// Algorithms for details.
private static final int ERR_VAL = 100;
public static BigInteger nextPrime(BigInteger start) {
if (isEven(start))
start = start.add(ONE);
else
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved