Next smallest prime palindrome
Given a positive integer N where
. The task is to find the smallest prime palindrome greater than or equal to N.
Examples:
Input: 8 Output: 11 Input: 7000000000 Output: 10000500001
Approach:
The Naive approach is to loop from N + 1 until we found next smallest prime palindrome greater than or equal to N.
Efficient Approach:
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:
Python3
# Python3 implementation of above approach import math # Function to check whether a number is prime def is_prime(n): return n > 1 and all(n % d for d in range(2, int(math.sqrt(n)) + 1)) # function to generate next smallest prime palindrome def NextprimePalindrome(N): for k in range(1, 10**6): # Check for odd-length palindromes s = str(k) x = int(s + s[-2::-1]) # eg. s = '1234' to x = int('1234321') if x >= N and is_prime(x): return x # Check for even-length palindromes s = str(k) x = int(s + s[-1::-1]) # eg. s = '1234' to x = int('12344321') if x >= N and is_prime(x): return x # Driver code N = 7000000000 # Function call to print answer print(NextprimePalindrome(N)) # This code is written by # Sanjit_Prasad |
10000500001
Time Complexity: O(N*sqrt(N)) where N is upper limit and the sqrt(N) term comes from checking if a candidate is prime.
Recommended Posts:
- Construct lexicographically smallest palindrome
- Smallest and Largest Palindrome with N Digits
- Given a number, find the next smallest palindrome
- Find next palindrome prime
- Make a lexicographically smallest palindrome with minimal changes
- Make lexicographically smallest palindrome by substituting missing characters
- Smallest prime divisor of a number
- Smallest prime number missing in an array
- Sum and product of k smallest and k largest prime numbers in the array
- Check if the first and last digit of the smallest number forms a prime
- Queries for the smallest and the largest prime number of given digit
- Smallest Special Prime which is greater than or equal to a given number
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Count occurrences of a prime number in the prime factorization of every element from the given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



