-
Notifications
You must be signed in to change notification settings - Fork 0
/
p043.java
42 lines (34 loc) · 1.11 KB
/
p043.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
package level02;
import java.util.List;
import org.junit.Test;
import lib.EulerTest;
public class p043 extends EulerTest {
final int B = 10;
final int K = 3;
final List<Integer> PRIMES = getPrimes(B - K);
/**
* Find the number of pandigital numbers [D1][D2]...[D10] such that the K-substrings are all
* divisible by a corresponding prime, i.e.
* <li>[D2][D3][D4] is divisible by 2.
* <li>[D3][D4][D5] is divisible by 3. <br>
* <li>...
*/
@Test
public void test() {
permutations(range(B)).generate(permutation -> {
if (hasSubstringDivisibilityProperty(permutation))
ans += Long.parseLong(join(permutation));
});
check(16695334890L);
}
boolean hasSubstringDivisibilityProperty(List<Integer> permutation) {
for (int i = 0; i < B - K; i++) {
int substring = 0;
for (int j = 0; j < K; j++)
substring += pow(B, K - 1 - j) * permutation.get(i + 1 + j);
if (substring % PRIMES.get(i) != 0)
return false;
}
return true;
}
}