iteratively build and test number on palindrome patterns
Intuition

all palindromes of even size has 11 as factor, so we can skip them except special case of 11.


iteratively build numbers in increasing order based o
# Intuition
- all palindromes of `even size` has `11` as factor, so we can skip them except special case of `11`.
- iteratively build numbers in increasing order based on palindrome patterns `1`, `101`, `10001`, `1000001`, `100000001`. It is much trickier than building regular decimal numbers from weights `1`, `10`, `100`, ..., but the idea is essentially the same.
- check a number `n` to be prime if no factors less than `sqrt(n)`

 
# Code
```cpp []
class Solution {
    bool prime(int n) {
        for(int a = sqrt(n); a > 1; --a) {
            if(n % a == 0)
                return false;
        }
        return n > 1;
    }
    int size(int n, int& msd) {
        int count = 0;
        for(; n; n /= 10) {
            count++;
            msd = n%10;
        }
        return count;
    }
    int pattern[5] = {1, 101, 10001, 1000001, 100000001};
    int weight[5]  = {0};
    int digit[5] = {0};
    int getnumber(int sz) {
        int a = 0;
        for(int i = 0; i <= sz/2; ++i) 
            a += digit[i] * weight[i] * pattern[i];
        return a;
    } 
    bool next(int sz, int i = 0) {
        if(i > sz/2)
            return false;

        if(++digit[i] == 10) {
            digit[i] = 0;
            return next(sz, ++i);
        }
        return true;
    }
public:
    int primePalindrome(int n) {
        int msd, sz = size(n, msd);
        digit[sz/2] = sz%2 ? msd : 1;
        if(sz % 2 == 0) ++sz;
        do {
            int j = sz/2;
            weight[j] = 1;
            for(int i = j-1; i >=0; --i)
                weight[i] = weight[i+1] * 10;

            //copy(weight, weight+5, ostream_iterator<int>(cout, ", ")); cout << endl;
            //copy(digit, digit+5, ostream_iterator<int>(cout, ", ")); cout << endl;

            while(true) {
                //cout << getnumber(sz) << endl;
                if(int a = getnumber(sz); a >= n && prime(a))
                    return 11 >= n ? min(11, a) : a;
                if(!next(sz)) {
                    sz += 2;
                    break;
                }
            }
        }
        while(sz <= 9);
        return {};
    }
};
```
['C++']