-
Notifications
You must be signed in to change notification settings - Fork 1
/
Problem051_ConsecutivePrimeSum.java
77 lines (59 loc) · 2.51 KB
/
Problem051_ConsecutivePrimeSum.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
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by Sneeuwpopsneeuw on 20-Aug-16.
*/
public class Problem051_ConsecutivePrimeSum extends Problem {
public static void main(String[] args) { new Problem051_ConsecutivePrimeSum().start(); }
private void start() {
for (int index=0; true; index ++) {
String str = "" + index;
for (int i=0; i < str.length(); i++)
check(str, i);
for (int i=0; i < str.length() - 1; i++)
for (int j=i+1; j < str.length(); j++)
check(str, i, j);
for (int i=0; i < str.length() - 2; i++)
for (int j=i+1; j < str.length() - 1; j++)
for (int k=j+1; k < str.length(); k++)
check(str, i, j, k);
}
}
private void check(String str, int... indexes) {
ArrayList<Integer> oldNumbers = new ArrayList<>();
char[] array = str.toCharArray();
for (char ch='0'; ch <= '9'; ch++) { // its nice that we can do this in java
for (int i : indexes)
array[i] = ch;
int number = Integer.parseInt(String.valueOf(array));
if (isPrime(number))
oldNumbers.add(number);
}
if (oldNumbers.size() == 8) {
if (arrayElementsSameStringLengh( oldNumbers.stream().mapToInt(i -> i).toArray() )) {
System.out.println("");
System.out.println(Arrays.toString(oldNumbers.toArray()));
throw new RuntimeException(); // we found something lets quit
}
}
}
private boolean arrayElementsSameStringLengh( int[] array ) {
int length = String.valueOf(array[0]).length();
for (int i=1; i < array.length; i++)
if (String.valueOf(array[i]).length() != length)
return false;
return true;
}
}
/*
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.
*/