866. 回文素数

求出大于或等于 N 的最小回文素数。

回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数

例如,2,3,5,7,11 以及 13 是素数。

回顾一下,如果一个数从左往右读与从右往左读是一样的,那么这个数是回文数。

例如,12321 是回文数。

 

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
101
package com711;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class Solution866 {
    public int primePalindrome(int N) {
        List<Integer> list = getPalindrome(N);
        Collections.sort(list);
        return list.get(0);
 
    }
 
    public List<Integer> getPalindrome(int N) {
        List<Integer> list = new ArrayList<Integer>();
        // 10以内的特例
        for (int i = N; i < 10; i++) {
            if (isPrime(i)) {
                list.add(i);
                return list;
            }
 
        }
        int start = getMinStart(N);
        // 双数的判断
        for (int i = (int) Math.pow(10, start); i < 10000; i++) {
            int num = Integer.parseInt(i + "" + reverse(i));
            if (num >= N && isPrime(num) && num < 200000000) {
                list.add(num);
                break;
            }
        }
 
        for (int i = (int) Math.pow(10, start); i < 10000; i++) {
            for (int j = 0; j <= 9; j++) {
                int num1 = Integer.parseInt(i + "" + j + reverse(i));
                if (num1 >= N && isPrime(num1) && num1 < 200000000) {                
                    list.add(num1);
                    return list;
                }
            }
 
        }
 
        return list;
    }
 
    public boolean isPrime(int n) {
        if (n == 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
 
    public String reverse(int n) {
        char[] chars = (n + "").toCharArray();
        int len = chars.length;
        for (int i = 0; i < len / 2; i++) {
            char temp = chars[i];
            chars[i] = chars[len - i - 1];
            chars[len - i - 1] = temp;
        }
        return new String(chars);
    }
 
    public boolean isHuiWen(int n) {
        char[] chars = (n + "").toCharArray();
        int len = chars.length;
        for (int i = 0; i < len; i++) {
            if (chars[i] != chars[len - i - 1]) {
                return false;
            }
        }
        return true;
 
    }
 
    public int getMinStart(int n) {
        String str = n + "";
        str = str.substring(0, str.length() / 2);
 
        return str.length() / 2;
    }
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Solution866 s = new Solution866();
 
        long start = System.currentTimeMillis();
        System.out.println(s.primePalindrome(85709140));
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
 
}

  本题最大的困扰是如果直接不找规律遍历的话,你肯定会超时。因此要用最快的方法,找到最近的回文数,去判断。