Find next palindrome prime
Find the smallest palindrome number which is prime too and greater then given number N.
Examples:
Input : N = 7 Output :11 11 is the smallest palindrome prime which is greater than N. Input : N = 112 Output : 131
A simple approach is to start a loop from N+1. For every number, check if it is palindrome and prime.
An efficient solution is based on below observations. All palindrome with even digits is multiple of 11.
We can prove as follow:
11 % 11 = 0
1111 % 11 = 0
111111 % 11 = 0
11111111 % 11 = 0
So:
1001 % 11 = (1111 – 11 * 10) % 11 = 0
100001 % 11 = (111111 – 1111 * 10) % 11 = 0
10000001 % 11 = (11111111 – 111111 * 10) % 11 = 0
For any palindrome with even digits:
abcddcba % 11
= (a * 10000001 + b * 100001 * 10 + c * 1001 * 100 + d * 11 * 1000) % 11
= 0
All palindrome with even digits is multiple of 11.
So among them, 11 is the only one prime
if (8 <= N <= 11) return 11
For other, we consider only palindrome with odd digits.
C++
// CPP program to find next palindromic // prime for a given number. #include <iostream> #include <string> using namespace std; bool isPrime(int num) { if (num < 2 || num % 2 == 0) return num == 2; for (int i = 3; i * i <= num; i += 2) if (num % i == 0) return false; return true; } int primePalindrome(int N) { // if(8<=N<=11) return 11 if (8 <= N && N <= 11) return 11; // generate odd length palindrome number // which will cover given constraint. for (int x = 1; x < 100000; ++x) { string s = to_string(x), r(s.rbegin(), s.rend()); int y = stoi(s + r.substr(1)); // if y>=N and it is a prime number // then return it. if (y >= N && isPrime(y)) return y; } return -1; } // Driver code int main() { cout << primePalindrome(112); return 0; } |
Java
// Java program to find next palindromic // prime for a given number. import java.lang.*; class Geeks { static boolean isPrime(int num) { if (num < 2 || num % 2 == 0) return num == 2; for (int i = 3; i * i <= num; i += 2) if (num % i == 0) return false; return true; } static int primePalindrome(int N) { // if(8<=N<=11) return 11 if (8 <= N && N <= 11) return 11; // generate odd length palindrome number // which will cover given constraint. for (int x = 1; x < 100000; ++x) { String s = Integer.toString(x); StringBuffer buffer = new StringBuffer(s); buffer.reverse(); int y = Integer.parseInt(s + buffer.substring(1).toString()); // if y>=N and it is a prime number // then return it. if (y >= N && isPrime(y) == true) return y; } return -1; } // Driver code public static void main(String args[]) { System.out.print(primePalindrome(112)); } } |
Python3
# Python3 program to find next palindromic # prime for a given number. import math as mt def isPrime(num): if (num < 2 or num % 2 == 0): return num == 2 for i in range(3, mt.ceil(mt.sqrt(num + 1))): if (num % i == 0): return False return True def primePalindrome(N): # if(8<=N<=11) return 11 if (8 <= N and N <= 11): return 11 # generate odd length palindrome number # which will cover given constraint. for x in range(1, 100000): s = str(x) d = s[::-1] y = int(s + d[1:]) # if y>=N and it is a prime number # then return it. if (y >= N and isPrime(y)): return y # Driver code print(primePalindrome(112)) # This code is contributed by # Mohit kumar 29 |
C#
// C# program to find next palindromic // prime for a given number. using System; using System.Text; using System.Collections; class Geeks { static bool isPrime(int num) { if (num < 2 || num % 2 == 0) return num == 2; for (int i = 3; i * i <= num; i += 2) if (num % i == 0) return false; return true; } static int primePalindrome(int N) { // if(8<=N<=11) return 11 if (8 <= N && N <= 11) return 11; // generate odd length palindrome number // which will cover given constraint. for (int x = 1; x < 100000; ++x) { string s = x.ToString(); char[] buffer = s.ToCharArray(); Array.Reverse(buffer); int y = Int32.Parse(s + new string(buffer).Substring(1)); // if y>=N and it is a prime number // then return it. if (y >= N && isPrime(y) == true) return y; } return -1; } // Driver code public static void Main() { Console.WriteLine(primePalindrome(112)); } } // This code is contributed by Mithun Kumar. |
PHP
=N and it is a prime number
// then return it.
if ($y >= $N && isPrime($y) == true)
return $y;
}
return -1;
}
// Driver code
print(primePalindrome(112));
// This code is contributed by mits
?>
131
Recommended Posts:
- Next smallest prime palindrome
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Given a number, find the next smallest palindrome
- Find the player who rearranges the characters to get a palindrome string first
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Find minimum number of merge operations to make an array palindrome
- Find two prime numbers with given sum
- Given a number x , find y such that x*y + 1 is not a prime
- Find the prime P using given four integers
- Find if nCr is divisible by the given prime
- Program to find sum of prime numbers between 1 to n
- Find the sum of non-prime elements in the given array
- Find product of prime numbers between 1 to n
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.



