-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp037.java
More file actions
39 lines (32 loc) · 906 Bytes
/
p037.java
File metadata and controls
39 lines (32 loc) · 906 Bytes
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
package level02;
import org.junit.Test;
import lib.EulerTest;
public class p037 extends EulerTest {
final int B = 10;
final int L = 1000000;
/**
* Find the sum of all truncatable primes with at least two digits where every prefix and suffix
* of the prime is also prime (e.g. 3797 is a truncatable prime because 3797, 797, 97, 7, 379,
* 37, and 3 are all prime.
*/
@Test
public void test() {
for (int p : primes(L))
if (isTruncatablePrime(p))
ans += p;
check(748317);
}
boolean isTruncatablePrime(int n) {
if (n < B)
return false;
for (int powB = B; powB < n; powB *= B)
if (!isPrime(n % powB))
return false;
while (n > 0) {
if (!isPrime(n))
return false;
n /= B;
}
return true;
}
}