- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
97 lines (79 loc) · 3.13 KB
/
Copy pathMain.java
File metadata and controls
97 lines (79 loc) · 3.13 KB
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
importjava.util.*;
/*
@author: mc-es
Problem 51
By replacing the 1st digit of the 2-digit number *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.
Answer: 121313
*/
publicclassMain {
publicstaticvoidmain(String[] args) {
System.out.println("Smallest prime number: " + findSmallestPrimeWithReplacements());
}
publicstaticList<Integer> generatePrimes(intlimit) {
boolean[] sieve = newboolean[limit];
Arrays.fill(sieve, true);
sieve[0] = sieve[1] = false;
for (inti = 2; i * i < limit; i++) {
if (sieve[i]) {
for (intmultiple = i * i; multiple < limit; multiple += i) {
sieve[multiple] = false;
}
}
}
List<Integer> primes = newArrayList<>();
for (inti = 0; i < limit; i++) {
if (sieve[i]) {
primes.add(i);
}
}
returnprimes;
}
publicstaticintfindSmallestPrimeWithReplacements() {
intlimit = 1_000_000;
Set<Integer> primes = newHashSet<>(generatePrimes(limit));
for (intprime : primes) {
StringprimeStr = String.valueOf(prime);
intlength = primeStr.length();
for (intnumDigits = 1; numDigits < length; numDigits++) {
List<List<Integer>> combinations = generateCombinations(length, numDigits);
for (List<Integer> indices : combinations) {
List<Integer> replacements = newArrayList<>();
for (chardigit = '0'; digit <= '9'; digit++) {
char[] candidate = primeStr.toCharArray();
for (intidx : indices) {
candidate[idx] = digit;
}
intnewNumber = Integer.parseInt(newString(candidate));
if (primes.contains(newNumber) && String.valueOf(newNumber).length() == length) {
replacements.add(newNumber);
}
}
if (replacements.size() >= 8) {
returnCollections.min(replacements);
}
}
}
}
return -1;
}
publicstaticList<List<Integer>> generateCombinations(intn, intr) {
List<List<Integer>> combinations = newArrayList<>();
combine(combinations, newArrayList<>(), 0, n, r);
returncombinations;
}
privatestaticvoidcombine(List<List<Integer>> combinations, List<Integer> current, intstart, intn, intr) {
if (current.size() == r) {
combinations.add(newArrayList<>(current));
return;
}
for (inti = start; i < n; i++) {
current.add(i);
combine(combinations, current, i + 1, n, r);
current.remove(current.size() - 1);
}
}
}