-
Notifications
You must be signed in to change notification settings - Fork 6
/
Seonghun.java
50 lines (42 loc) · 919 Bytes
/
Seonghun.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
import java.util.Arrays;
import java.util.Scanner;
public class Seonghun {
static int N;
static final int MAX = 1003001;
static boolean[] prime = new boolean[MAX + 1];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// initialize
N = input.nextInt();
input.close();
eratos();
// solution
for (int n = N; n <= MAX; n++) {
if (prime[n] && isPalindrome(n)) {
System.out.println(n);
break;
}
}
}
static void eratos() {
Arrays.fill(prime, true);
prime[1] = false;
for (int i = 2; i * i <= MAX; i++) {
if (prime[i]) {
for (int j = 2 * i; j <= MAX; j += i) {
prime[j] = false;
}
}
}
}
static boolean isPalindrome(int n) {
String s = Integer.toString(n);
int len = s.length();
for (int i = 0; i < len / 2; i++) {
if (s.charAt(i) != s.charAt(len - i - 1)) {
return false;
}
}
return true;
}
}