Given a positive integer N where . The task is to find the smallest prime palindrome greater than or equal to N.
Input: 8 Output: 11 Input: 7000000000 Output: 10000500001
The Naive approach is to loop from N + 1 until we found next smallest prime palindrome greater than or equal to N.
Lets say P = R is a the next smallest prime-palindrome greater than or equal to N.
Now since R is a palindrome, the first half of the digits of R can be used to determine R up-to two possibilities. Let k be the first half of the digits in R. For eg. if k = 123, then R = 12321 or R = 123321.
Thus we iterate through each k upto 105 and create the associated palindrome R, and check whether R is a prime or not.
Also we will handle the odd and even palindromes separately, and break when we fount our result.
Below is the implementation of above approach:
Time Complexity: O(N*sqrt(N)) where N is upper limit and the sqrt(N) term comes from checking if a candidate is prime.
- Find next palindrome prime
- Given a number, find the next smallest palindrome
- Smallest and Largest Palindrome with N Digits
- Construct lexicographically smallest palindrome
- Make a lexicographically smallest palindrome with minimal changes
- Smallest prime divisor of a number
- Smallest prime number missing in an array
- Check if the first and last digit of the smallest number forms a prime
- Smallest Special Prime which is greater than or equal to a given number
- Sum and product of k smallest and k largest prime numbers in the array
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Print the nearest prime number formed by adding prime numbers to N
- Quick ways to check for Prime and find next Prime in Java
- Print prime numbers with prime sum of digits in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.