-
Notifications
You must be signed in to change notification settings - Fork 0
/
p035.java
35 lines (28 loc) · 818 Bytes
/
p035.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
package level02;
import org.junit.Test;
import lib.EulerTest;
public class p035 extends EulerTest {
final int B = 10;
final int L = 1000000;
/**
* Find the sum of all circular primes, primes where all rotations of the prime are prime (e.g.
* 197 is a circular prime because 197, 971, and 719 are prime.
*/
@Test
public void test() {
for (int p : primes(L))
if (isCircularPrime(p))
ans++;
check(55);
}
boolean isCircularPrime(int n) {
int numDigits = Integer.toString(n, B).length();
int scale = ipow(B, numDigits - 1);
for (int i = 1; i < numDigits; i++) {
n = (n % scale) * B + n / scale;
if (!isPrime(n))
return false;
}
return true;
}
}