-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimePalindrome.cpp
More file actions
38 lines (36 loc) · 1.08 KB
/
primePalindrome.cpp
File metadata and controls
38 lines (36 loc) · 1.08 KB
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
// Source: https://leetcode.com/problems/prime-palindrome/
// Author: Miao Zhang
// Date: 2021-03-19
class Solution {
public:
int primePalindrome(int N) {
for (int l = 1; l < 6; l++) {
for (int num = pow(10, l - 1); num < pow(10, l); num++) {
string s = to_string(num);
for (int k = l - 2; k >= 0; k--) {
s.push_back(s[k]);
}
int x = stoi(s);
if (x >= N && isPrime(x)) return x;
}
for (int num = pow(10, l - 1); num < pow(10, l); num++) {
string s = to_string(num);
for (int k = l - 1; k >= 0; k--) {
s.push_back(s[k]);
}
int x = stoi(s);
if (x >= N && isPrime(x)) return x;
}
}
return -1;
}
private:
bool isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
for (int i = 2; i < sqrt(n) + 1; i++) {
if (n % i == 0) return false;
}
return true;
}
};