Given an integer n, return the smallest prime palindrome greater than or equal to n.
An integer is prime if it has exactly two divisors: 1 and itself. Note that 1 is not a prime number.
2, 3, 5, 7, 11, and 13 are all primes.An integer is a palindrome if it reads the same from left to right as it does from right to left.
101 and 12321 are palindromes.The test cases are generated so that the answer always exists and is in the range [2, 2 * 108].
Example 1:
Input: n = 6 Output: 7
Example 2:
Input: n = 8 Output: 11
Example 3:
Input: n = 13 Output: 101
Constraints:
1 <= n <= 108When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding a prime palindrome involves checking every number within a specified range. We test each number to see if it's both a prime number and a palindrome. If it is, we've found a potential solution.
Here's how the algorithm would work step-by-step:
def find_largest_prime_palindrome_brute_force(max_number):
largest_prime_palindrome = 0
for current_number in range(2, max_number + 1):
# First, check if the number is a palindrome
if str(current_number) == str(current_number)[::-1]:
# If it is a palindrome, then check if it is prime
is_prime = True
for possible_divisor in range(2, int(current_number**0.5) + 1):
if current_number % possible_divisor == 0:
is_prime = False
break
#If current number is prime, update result
if is_prime:
largest_prime_palindrome = current_number
return largest_prime_palindromeTo find the largest prime palindrome under a given number, we want to avoid checking every number. Instead, we will generate palindromes and then check if they are prime, focusing on larger palindromes first since we want the largest one.
Here's how the algorithm would work step-by-step:
def prime_palindrome(number):
for digits in range(len(str(number)), 0, -1):
# Skip even-digit palindromes as they are divisible by 11.
if digits % 2 == 0:
continue
start = 10**(digits // 2)
end = 10**(digits // 2 + 1)
for i in range(end - 1, start - 1, -1):
palindrome = int(str(i) + str(i)[:digits // 2][::-1])
if palindrome >= number:
continue
if is_prime(palindrome):
return palindrome
# Return 7 if no prime palindrome is found.
return 7
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True| Case | How to Handle |
|---|---|
| Input 'n' is less than 2 | Return an empty list because no single-digit or zero-digit prime palindromes exist. |
| Large input 'n' approaching maximum integer value | Use an efficient prime checking algorithm and palindrome generation technique to avoid excessive computation and potential timeouts. |
| No prime palindrome exists within the specified range [2, n] | Return an empty list to indicate that no valid prime palindrome was found. |
| Integer overflow during palindrome generation or prime checking | Use a data type that can accommodate larger numbers (e.g., long) or implement checks to prevent overflow. |
| Input 'n' is a prime palindrome itself | Include 'n' in the returned list if it satisfies both the prime and palindrome properties. |
| Memory constraints when generating a large sequence of numbers to check | Optimize the algorithm to avoid storing all numbers in memory at once, checking primality and palindromicity on the fly. |
| Input 'n' is a negative number | Return an empty list or throw an exception, as prime numbers are not defined for negative values. |
| The upper limit n contains only one repeated digit, for example 1111 | Efficiently skip these ranges to improve performance since many composite palindromes consist of repeated digits. |