Fermat's little theorem to check odd palindrome for Prime in O(log n) time
\nclass Solution {\n    int binPow(int num, int p, int m){\n        long long int ans=1;\n        long long int n=num;\n        while (p){\n            if (p&1)
```\nclass Solution {\n    int binPow(int num, int p, int m){\n        long long int ans=1;\n        long long int n=num;\n        while (p){\n            if (p&1) ans=((1LL*ans%m)*(n%m))%m;\n            n = ((1LL*n%m)*(n%m))%m;\n            p/=2;\n        }\n        return ans;\n    }\n    bool fermatPrime(int p){\n        if (p<=1) return false;\n        if (p==2 || p==3 || p==5 or p==7 or p==11) return true;\n        for (int i=1;i<=5;i++){\n            int a=2+rand()%(p-3);\n            if (binPow(a,p-1,p)!=1) return false;\n        }\n        return true;\n    }\n    int oddPal(int num){\n        int n=num;\n        n/=10;\n        while (n){\n            int rem=n%10;\n            num=num*10+rem;\n            n/=10;\n        }\n        return num;\n    }\npublic:\n    int primePalindrome(int N) {\n        set<int> tab;\n        for (int i=1;i<=2*1e4+7;i++){\n            if (i==11){\n                tab.insert(11);\n                continue;\n            }\n            int p=oddPal(i);\n            if (fermatPrime(p)) tab.insert(p);\n        }\n        auto idx=tab.lower_bound(N);\n        return *idx;\n    }\n};\n```
[]