-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathProblem_35.java
More file actions
36 lines (34 loc) · 680 Bytes
/
Problem_35.java
File metadata and controls
36 lines (34 loc) · 680 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
class Problem_35 {
public static void main(String[] args){
int count = 0;
for(int i = 2; i < 1000000; i++) {
if(isPrime(i) && isCircular(i)) {
count++;
}
}
System.out.println(count);
}
public static boolean isPrime(int n) {
if(n == 2) {
return true;
}
if(n == 1 || n % 2 == 0) {
return false;
}
for(int i = 3; i * i <= n; i += 2) {
if(n % i == 0) {
return false;
}
}
return true;
}
public static boolean isCircular(int num) {
String s = Integer.toString(num);
for (int i = 0; i < s.length(); i++) {
if (!isPrime(Integer.parseInt(s.substring(i) + s.substring(0, i)))) {
return false;
}
}
return true;
}
}