Skip to content

Instantly share code, notes, and snippets.

@SuryaPratapK
Created March 25, 2024 15:29
Show Gist options
  • Save SuryaPratapK/57d6ddedd55f7226a623e6a56a3ed263 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/57d6ddedd55f7226a623e6a56a3ed263 to your computer and use it in GitHub Desktop.
class Solution {
bool isPalindrome(int no){
string s = to_string(no);
int left=0,right=s.size()-1;
while(left<right){
if(s[left]!=s[right])
return false;
left++;
right--;
}
return true;
}
bool findPrime(int no){
bool isPrime = true;
for(int i=2;i*i<=no;++i)
if(no%i==0){
isPrime = false;
break;
}
return isPrime;
}
public:
int primePalindrome(int n) {
int no = n==1?2:n;
while(1){
if((no>1e3 and no<1e4) or
(no>1e5 and no<1e6) or
(no>1e7 and no<1e8)){
no = pow(10,ceil(log10(no)));;
continue;
}
if(findPrime(no) and isPalindrome(no))
return no;
no++;
}
return -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment