To find the smallest prime palindrome greater than or equal to n, generate palindromic numbers (pal) and check if they are prime. Brute-forcing every integer is too slow for large n, so we efficiently generate palindromes and test each pal for primality. We prioritize odd-length palindromes because even-length palindromes (except 11) are divisible by 11 and thus not prime.
For each palindrome len (number of digits) starting from the length of n, generate palindromes by constructing a root (the left half) and reflecting it to form pal.
For each root in the range start..end-1, build left = string of root and right = reverse of left without its last digit, then pal = int(left + right).
If pal >= n and pal is prime, return pal.
Special cases: return 2 if n <= 2; return 11 if 8 <= n <= 11 (since even-length palindromes aren’t prime except 11).
boolisPrime(int x) {
if (x <2) return false;
if (x ==2) return true;
if (x %2==0) return false;
for (int i =3; i * i <= x; i +=2) {
if (x % i ==0) return false;
}
return true;
}
intprimePalindrome(int n) {
if (n <=2) return2;
if (n >=8&& n <=11) return11;
for (int len =1; len <=9; ++len) {
int start = pow(10, (len -1) /2), end = pow(10, (len +1) /2);
for (int root = start; root < end; ++root) {
string left = to_string(root);
string right = left.substr(0, left.size() -1);
reverse(right.begin(), right.end());
int pal = stoi(left + right);
if (pal >= n && isPrime(pal)) return pal;
}
}
return-1;
}
funisPrime(x: Int): Boolean {
if (x < 2) returnfalseif (x ==2) returntrueif (x % 2==0) returnfalsevar i = 3while (i * i <= x) {
if (x % i ==0) returnfalse i +=2 }
returntrue}
funprimePalindrome(n: Int): Int {
if (n <=2) return2if (n in8..11) return11for (len in1..9) {
val start = Math.pow(10.0, ((len - 1) / 2).toDouble()).toInt()
val end = Math.pow(10.0, ((len + 1) / 2).toDouble()).toInt()
for (root in start until end) {
val left = root.toString()
val right = left.substring(0, left.length - 1).reversed()
val pal = (left + right).toInt()
if (pal >= n && isPrime(pal)) return pal
}
}
return -1}
defis_prime(x: int) -> bool:
if x <2:
returnFalseif x ==2:
returnTrueif x %2==0:
returnFalse i =3while i * i <= x:
if x % i ==0:
returnFalse i +=2returnTruedefprimePalindrome(n: int) -> int:
if n <=2:
return2if8<= n <=11:
return11for length in range(1, 10):
start =10** ((length -1) //2)
end =10** ((length +1) //2)
for root in range(start, end):
left = str(root)
right = left[:-1][::-1]
pal = int(left + right)
if pal >= n and is_prime(pal):
return pal
return-1
fnis_prime(x: i32) -> bool {
if x <2 {
returnfalse;
}
if x ==2 {
returntrue;
}
if x %2==0 {
returnfalse;
}
letmut i =3;
while i * i <= x {
if x % i ==0 {
returnfalse;
}
i +=2;
}
true}
fnprime_palindrome(n: i32) -> i32 {
if n <=2 {
return2;
}
if n >=8&& n <=11 {
return11;
}
for len in1..=9 {
let start =10_i32.pow(((len -1) /2) asu32);
let end =10_i32.pow(((len +1) /2) asu32);
for root in start..end {
let left = root.to_string();
let right: String = left.chars().take(left.len() -1).rev().collect();
let pal =format!("{}{}", left, right).parse::<i32>().unwrap();
if pal >= n && is_prime(pal) {
return pal;
}
}
}
-1}
⏰ Time complexity: O(sqrt(N) * logN) for each palindrome checked, but the number of palindromes generated is much less than N. The overall runtime is efficient for the given constraints.
🧺 Space complexity: O(1) (ignoring output and call stack for string operations).