package org.psjava.algo.math.numbertheory;

import org.psjava.ds.array.Array;

/**
 * Almost same with {@link PrimalityTesterByDivision}. But if there are prepared primes, we can do division-test only for primes.
 * 
 * Then, the time is almost 10 times faster than {@link PrimalityTesterByDivision}.
 * 
 * Here, we uses prime number sieve to get prime number list.
 */

public class PrimalityTesterByPreparedPrimeDivision {

	public static PrimalityTester getInstance(long max, PrimeNumberSieve sieve) {
		final Array<Integer> primes = sieve.calcList((int) Math.sqrt(max) + 1);
		return new PrimalityTester() {

			@Override
			public boolean isPrime(long v) {
				if (v <= 1)
					return false;
				for (long p : primes) {
					if (p * p <= v) {
						if (v % p == 0)
							return false;
					} else {
						break;
					}
				}
				return true;
			}
		};
	}

	private PrimalityTesterByPreparedPrimeDivision() {
	}

}

@@ -623,18 +623,16 @@
 er() %7B%0D%0A
-%0D%0A
 %09%09%09@Over
@@ -759,18 +759,28 @@
 * p 
-%3C=
+%3E
  v)
- %7B
 %0D%0A%09
+%09%09%09%09%09break;%0D%0A
 %09%09%09%09
@@ -799,25 +799,24 @@
 = 0)%0D%0A%09%09%09%09%09%09
-%09
 return false
@@ -822,45 +822,8 @@
 e;%0D%0A
-%09%09%09%09%09%7D else %7B%0D%0A%09%09%09%09%09%09break;%0D%0A%09%09%09%09%09%7D%0D%0A
 %09%09%09%09

make code simpler