-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem_043.java
More file actions
48 lines (37 loc) · 1.14 KB
/
Problem_043.java
File metadata and controls
48 lines (37 loc) · 1.14 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
package euler;
import java.util.ArrayList;
public class Problem_043 {
static long substringDivisabilitySum() {
return substringDivisabilitySum(get10Pandigitals());
}
private static long substringDivisabilitySum(ArrayList<String> pandigitals) {
long sum = 0;
for (String pandigital : pandigitals) {
if (substringIsDivisible(pandigital))
sum += Long.parseLong(pandigital);
}
return sum;
}
private static boolean substringIsDivisible(String pandigital) {
int[] firstSevenPrimes = { 2, 3, 5, 7, 11, 13, 17 };
for (int i = 0; i < firstSevenPrimes.length; i++)
if (Long.parseLong(pandigital.substring(i + 1, i + 4)) % firstSevenPrimes[i] != 0)
return false;
return true;
}
private static ArrayList<String> get10Pandigitals() {
ArrayList<String> perms = new ArrayList<String>();
perms.add("10");
perms.add("01");
for(int i = 2; i < 10; i++) {
ArrayList<String> next = new ArrayList<String>(perms);
perms.clear();
for(int j = 0; j <= i; j++) {
for(int k = 0; k < next.size(); k++) {
perms.add(new StringBuilder(next.get(k)).insert(j, i).toString());
}
}
}
return perms;
}
}