Project Euler – Problem 51

Problem 51:
By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

import java.util.HashSet;
 
class runner
{
	/* 
	 * Sieve to determine composite/prime numbers
	 * My attempt at optimizing mainly is made up of checking values of 
	 *    n*n, n*n+2*n, .. length 
	 * This reduces the execution time for arr.length=1mil from 30ms to 16ms.
	 */
	private static int sieveArray(boolean[] arr){
		arr[0]=true;arr[1]=true;
		int c=2;
		for(int j = 4; j < arr.length; j+=2){
			arr[j] = true;
			c++;
		}
 
		for(int i=3;i<Math.sqrt(arr.length);i+=2){
			if(arr[i]) continue;
			int ii = 2 * i;
			for(int j = i*i; j < arr.length; j+=ii){
				if(arr[j]) continue;
				arr[j] = true;
				c++;
			}
		}
		return arr.length - c;
	}
	//private static int[] convertArr(int n, boolean[] arr2){
	private static HashSet<String> convertArr(int n, boolean[] arr2){
		//int[] arr = new int[n];
		HashSet<String> arr = new HashSet<String>();
		//int k=0;
		for(int i=0;i<arr2.length;i++){
			if(!arr2[i]){
				//arr[k++] = i;
				arr.add(i+"");
			}
		}
		return arr;
	}
 
	public static void main (String[] args) throws java.lang.Exception
	{
		long time = System.currentTimeMillis();
 
		boolean[] primesieve = new boolean[1000000];//000
		int numOfPrimes = sieveArray(primesieve);
		System.out.println("sieveArray time: "+(System.currentTimeMillis() - time));
 
		//Used HashSet to exploit the log(n) "contains" check speed.
		HashSet<String> primes = convertArr(numOfPrimes, primesieve);
		System.out.println("convertArr time: "+(System.currentTimeMillis() - time));
 
		primesieve = null;
		System.gc();
 
		int lfamily = 0;
		String lp = "9999999999";
		for(String p: primes){//For Each Prime			
			for(int i=0;i<10;i++){//iterate from 0..9
				int family = 1;
 
				for(int j=1;j<10;j++){//Push 0+1... 0+9 [1..9] 
					char newChar = (char)(48+((i+j)%10));
 
					boolean found = false;
					StringBuffer check = new StringBuffer();
 
					for(int k=0;k<p.length();k++){//For Each Character in String
						if(p.charAt(k) == i+48){
							check.append(newChar);
							found = true;
						}else{
							check.append(p.charAt(k));
						}
					}
 
					if(found && primes.contains(check.toString())){
						family++;
					}
				}
 
				if(family > lfamily){
					lfamily = family;
					lp=p;
				}else if(family == lfamily){
					if(p.compareTo(lp) < 0){
						lp = p;
					}
				}
			}
		}
		System.out.println("lp:"+lp+" len:"+lfamily);
		System.out.println("time: "+(System.currentTimeMillis() - time));
	}
}