public class Main {
	public static Primes p;

	// Four keywords:
	//   public, private, protected, NOTHING
	public static void main(String[] stuff) {

		// GENERAL STRATEGY
		// Memoize the primes; if there is a need for a bigger prime than is
		// memoized, the Primes object will do its buildPrimes method to
		// memoize whatever number of primes is needed.

		System.out.println();
		p = new Primes(10);
		System.out.println(p.get());
		System.out.println(p.isPrime(37));
		System.out.println(p.get());
		System.out.println(p.isPrime(2701));
		System.out.println(p.get());
		Brilliant b = new Brilliant(20);  // stores arraylist of 20 brilliant numbers
		System.out.println(b.getBrilliants());
	}

	// Static: One copy for the whole class
	public static boolean isPrime(int n) {

		int firstFactor = 2;
		for (int factor = firstFactor; factor <= Math.sqrt(n)+.01; factor++) {
			if (n % factor == 0) {
				return false;
			}
		}
		return true;
	}

	public static boolean isBrilliant(int n) {
		if (p.isPrime(n)) return false;

		if (n > p.get(0)) p.buildPrimes(n);

		for (int i = p.size()-1; i >= 0; i--) {
			// Is there a factor in the primes list?
			if (n % p.get(i) == 0) {
				int factor = n / p.get(i);
				// We have two factors:
				// p.get(i) is a prime because it's in the primes ArrayList
				// factor may or may not be a prime. If it is, then test for
				//   the same number of digits. If it's not, then the number is
				//   not a brilliant number.
				if (p.isPrime(factor)) {
					return (sameNumberOfDigits(factor, p.get(i)));
				} else {
					return false;
				}
			}
		}

		return false;
	}

	public static boolean sameNumberOfDigits(int n1, int n2) {
		return numDigits(n1) == numDigits(n2);
	}

	public static int numDigits(int n) {
		return ("" + n).length();
	}
}