# 866. Prime Palindrome

Find the smallest prime palindrome greater than or equal to `N`.

Recall that a number is  prime  if it’s only divisors are 1 and itself, and it is greater than 1.

For example, 2,3,5,7,11 and 13 are primes.

Recall that a number is a  palindrome  if it reads the same from left to right as it does from right to left.

For example, 12321 is a palindrome.

Example 1:

``````Input: 6
Output: 7
``````

Example 2:

``````Input: 8
Output: 11
``````

Example 3:

``````Input: 13
Output: 101
``````

Note:

• `1 <= N <= 10^8`
• The answer is guaranteed to exist and be less than `2 * 10^8`.

``````class Solution {
public:
int primePalindrome(int N) {
if (N >= 8 && N <= 11) return 11;
for (int i = 1; i < 1e5; ++i) {
string s = to_string(i), t(s.rbegin(), s.rend());
int x = stoi(s + t.substr(1));
if (x >= N && isPrime(x)) return x;
}
return -1;
}
bool isPrime(int num) {
if (num < 2 || num % 2 == 0) return num == 2;
int limit = sqrt(num);
for (int i = 3; i <= limit; ++i) {
if (num % i == 0) return false;
}
return true;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/866

Palindrome Number

Count Primes

https://leetcode.com/problems/prime-palindrome/

https://leetcode.com/problems/prime-palindrome/discuss/146798/Search-Palindrome-with-Odd-Digits

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation