Java tutorial
//package util; import java.util.ArrayList; import java.util.List; public class IsPrimeOld { private static List<Long> primeList = new ArrayList<Long>(); private static long maxNumber = 3; private static long newMaxNumber = 0; private boolean isPrimer; private long number; public IsPrimeOld(long number) { this.number = number; if (number == 2L || number == 3L) { this.isPrimer = true; } else if (number < maxNumber) { this.isPrimer = initIsPrimer(); } else { newMaxNumber = number; initPrimeList(); this.isPrimer = initIsPrimer(); } } private static void initPrimeList() { long start = System.currentTimeMillis(); if (maxNumber == 3) { primeList.add(2L); primeList.add(3L); } for (long i = maxNumber; i < newMaxNumber; i += 2) { long j = 1; double temp = Math.sqrt(i); for (j = 2; j < temp; j++) { if (i % j == 0) break; } if (j == (long) temp + 1) { primeList.add(i); } } long end = System.currentTimeMillis(); maxNumber = newMaxNumber; } private boolean initIsPrimer() { long start = System.currentTimeMillis(); boolean b = true; for (Long primer : primeList) { if (number % primer == 0) { b = false; break; } } long end = System.currentTimeMillis(); return b; } }