-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathprimes.java
101 lines (86 loc) · 2.51 KB
/
primes.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.*;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class primes {
public static void main(String []args) throws IOException {
Scanner in = new Scanner(System.in);
OutputWriter out = new OutputWriter(System.out);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
}
class Task{
public void solve(Scanner in, OutputWriter out) throws IOException{
String t = in.next();
BigInteger tt = new BigInteger(t);
if (tt.isProbablePrime(40)) {
for (int i = 0; i < t.length(); i++) {
char k = t.charAt(i);
if (k == '3' || k == '4' || k == '7') {
out.print("no\n");
return;
}
}
t = t.replace('6', 'x');
t = t.replace('9', '6');
t = t.replace('x', '9');
t = new StringBuilder(t).reverse().toString();
BigInteger t2 = new BigInteger(t);
if (t2.isProbablePrime(40)) {
out.print("yes\n");
} else {
out.print("no\n");
}
} else {
out.print("no\n");
}
}
}
class Scanner{
public BufferedReader reader;
public StringTokenizer st;
public Scanner(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next(){
while(st == null || !st.hasMoreTokens()){
try{
String line = reader.readLine();
if(line == null) return null;
st = new StringTokenizer(line);
}catch (Exception e){
throw (new RuntimeException());
}
}
return st.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
class OutputWriter{
BufferedWriter writer;
public OutputWriter(OutputStream stream){
writer = new BufferedWriter(new OutputStreamWriter(stream));
}
public void print(int i) throws IOException {
writer.write(i);
}
public void print(String s) throws IOException {
writer.write(s);
}
public void print(char []c) throws IOException {
writer.write(c);
}
public void close() throws IOException {
writer.close();
}
}