-
Notifications
You must be signed in to change notification settings - Fork 0
/
truncatableprimes.java
60 lines (55 loc) · 1.04 KB
/
truncatableprimes.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
public class truncatableprimes {
static boolean[] ba = new boolean[1000000];
public static void main(String[] args){
boolean b = false;
Set<Integer> al = new HashSet<Integer>();
int amount = 0;
int sum = 0;
int i = 10;
while(amount < 11)
{
if(pTest(i))
b = truncate(i);
if (b){
amount++;
sum+=i;
al.add(i);
b = false;
System.out.println(i);
}
i++;
System.out.println(i);
}
System.out.println(al);
System.out.println(sum);
}
public static boolean pTest(int n){
if (n == 1)
return false;
if (n == 2)
return true;
for (int k = 2; k <= n/2; k++){
if (n%k==0){
return false;
}
}
return true;
}
public static boolean truncate(int n){
int n1 = n;
int n2 = n;
int ten = (int) Math.log10((double) n);
for (int i = ten; i > 0; i--){
ten = (int) Math.pow(10, i);
n1 = n%ten;
n2 = n2/10;
if(!pTest(n1)||!pTest(n2))
return false;
}
return true;
}
}