Project Euler(51)
Posted on Wednesday, May 27th, 2009 at 5:16 am by Universe QueenProblem 51:
By replacing the 1^(st) digit of *57, it turns out that six of the possible values: 157, 257, 457, 557, 757, and 857, are all prime.
By replacing the 3^(rd) and 4^(th) digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes, 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.
Answer: 121313
Implementation in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | public class Euler51 { public static boolean isPrime( int number ) { if( number < 2 ) { return false; } else if ( number == 2 ) { return true; } else { for( long i=2; i<=Math.sqrt(number); i++ ) { if( number%i == 0 ) { return false; } } } return true; } public static int factory( int n ) { int result = 1; for( int i=1; i<=n; i++ ) { result *= i; } return result; } public static int[][] getCombinations( int n, int[] array ) { int[][] result; if( n == 1 ) { result = new int[array.length][1]; for( int i=0; i<array.length; i++ ) { result[i][0] = array[i]; } } else if( n == array.length ) { result = new int[1][]; result[0] = array.clone(); } else { int numberOfCombinations = factory(array.length) / ( factory(n) * factory(array.length-n) ); result = new int[numberOfCombinations][n]; int index = 0; for( int i=0; i<=array.length-n; i++ ) { int[] a = new int[array.length-i-1]; for( int j=0; j<a.length; j++ ) { a[j] = array[i+1+j]; } int[][] left = getCombinations( n-1, a ); for( int j=0; j<left.length; j++ ) { result[index][0] = array[i]; for( int m=0; m<left[j].length; m++ ) { result[index][m+1] = left[j][m]; } index++; } } } return result; } public static void main(String[] args) { int result = 0; int counter = 0; while( true ) { if( isPrime( result ) ) { String number = String.valueOf( result ); for( int numberOfSameDigiters=2; numberOfSameDigiters<=number.length(); numberOfSameDigiters++ ) { for( int index=0; index<=number.length()-numberOfSameDigiters; index++ ) { if( number.charAt( index ) - '0' > 2 ) { continue; } int[] indexes; int c = 0; for( int i=index+1; i<number.length(); i++ ) { if( number.charAt(i) == number.charAt(index) ) { c++; } } indexes = new int[c]; c = 0; for( int i=index+1; i<number.length(); i++ ) { if( number.charAt(i) == number.charAt(index) ) { indexes[c] = i; c++; } } int[][] p = getCombinations( numberOfSameDigiters-1, indexes ); for( int m=0; m<p.length; m++ ) { int v = number.charAt(index) - '0' + 1; char[] s = new String( number ).toCharArray(); while( v <= 9 ) { char z = (char)(v + '0'); s[0] = z; for( int b=0; b<p[m].length; b++ ) { s[p[m][b]] = z; } int y = Integer.valueOf( new String(s) ); if( isPrime(y) ) { counter++; } v++; } if( counter >= 7 ) { System.out.println( result ); return; } else { counter = 0; } } } } } result++; } } } |
