-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCircularPrimes.java
More file actions
35 lines (28 loc) · 817 Bytes
/
CircularPrimes.java
File metadata and controls
35 lines (28 loc) · 817 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
package problem35;
import java.util.List;
import problem07.PrimeNumbers;
public class CircularPrimes {
private static List<Integer> primes = SieveEratosthenes.getPrimeArray(1000000);
public static void main(String[] args) {
int count = 0;
for (int i: primes) {
if (isCircular(i)) {
count++;
}
}
System.out.println(count);
}
private static boolean isCircular(int n) {
String shift = "" + n;
if (shift.length() == 1) {
return true;
}
for (int i = 0; i < shift.length() - 1; i++) {
shift = (shift.charAt(shift.length() - 1) + shift).substring(0, shift.length());
if (!PrimeNumbers.primeTest(Integer.parseInt(shift))) {
return false;
}
}
return true;
}
}