-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution37.java
More file actions
48 lines (47 loc) · 1.46 KB
/
Copy pathSolution37.java
File metadata and controls
48 lines (47 loc) · 1.46 KB
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
43
44
45
46
47
48
public class Solution37{
static boolean isPrime(int num){
if(num <= 1){
return false;
}
else if(num <= 3){
return true;
}
else if(num % 2 == 0 || num % 3 ==0){
return false;
}
else{
//Note that any primes greater than 2 or 3 can be expressed in the form 6k+1 or 6k - 1.
for(int i = 5; i <= Math.sqrt(num);i+=6){
//Check if num is divisible by 6k + 1 or 6k - 1.
if(num % i == 0 || num % (i + 2) == 0){
return false;
}
}
return true;
}
}
public static void main(String[] args) {
int count = 0;
int i = 23;
int result = 0;
while(count < 11){
if(isPrime(i)){
boolean isTruncatable = true;
for(int d = 10; d < i; d *= 10){
int leftToRight = i % d;
int rightToLeft = i / d | 0;
if(!isPrime(leftToRight) || !isPrime(rightToLeft)){
isTruncatable = false;
}
}
if(isTruncatable){
count++;
result += i;
}
}
//iterate over all odd numbers greater than 23, since 23 is the first left and right-truncatable prime.
i+=2;
}
System.out.println(result);
}
}