I have seen previous solutions to this problem, but it is all complete search with a check function and not fast enough for me.
I am working on a C++ program trying to generate all prime palindromes highly efficiently in a given range of integers. For the prime part, I have created a fast primality tester by cutting down on divisors I test by eliminating all multiples of 2 and 3, though suggestions for improvement here would also be appreciated (function pasted below).
My main issue is that I need to generate palindromes fast enough without using the conventional complete search and palindrome test that slowly increments the tested integer. My current search code and primality test are pasted below.
I have tried to increment the digits of a number in the middle then the outer ones, but because over time more digits are added over time I couldn't even piece together an algorithm.
Primality Test:
bool CheckPrime(int n){
switch (n) {
case 1: return false; break;
case 2: return true; break;
case 3: return true; break;
default: break;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (int i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
Palindrome Test + Main Function
bool CheckPalindrome (int n) {
string temp = to_string(n);
reverse(temp.begin(), temp.end());
if (temp.compare((to_string(n))) == 0) {
return true;
}
else {
return false;
}
}
int main() {
int L, R; cin >> L >> R;
vector <int> Palindromes;
for (int i = L; i <= R; i++) {
if (CheckPalindrome(i)) {
Palindromes.push_back(i);
}
}
}