Pointer-Prime Number is a prime number p such that the next prime after p can be obtained from p by adding the product of the digits of p.
Some Pointer primes are:
23, 61, 1123, 1231, 1321, 2111, 2131, 11261….
Check if N is a Pointer prime number
Given a number N, the task is to check if N is a Pointer-Prime Number or not. If N is a Pointer-Prime Number then print “Yes” else print “No”.
Examples:
Input: N = 23
Output: Yes
Explanation:
23 + product of digits of 23 = 29,
which is the next prime after 23.
Input: N = 29
Output: No
Approach: :
- Find the product of digits of N
- Then, find the next prime number to N
- Now if N is prime and N + product of digits of N equals next prime to N, then print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
// C++ implementation for the // above approach #include <bits/stdc++.h> using namespace std; // Function to find the product of // digits of a number N int digProduct( int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function that returns true if n // is prime else returns false bool isPrime( int n) { // Corner cases if (n <= 1) return false ; if (n <= 3) return true ; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false ; for ( int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false ; return true ; } // Function to return the smallest // prime number greater than N int nextPrime( int N) { // Base case if (N <= 1) return 2; int prime = N; bool found = false ; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true ; } return prime; } // Function to check Pointer-Prime numbers bool isPointerPrime( int n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true ; else return false ; } // Driver Code int main() { // Given Number N int N = 23; // Function Call if (isPointerPrime(N)) cout << "Yes" ; else cout << "No" ; return 0; } |
Java
// Java program for above approach class GFG{ // Function to find the product of // digits of a number N static int digProduct( int n) { int product = 1 ; while (n != 0 ) { product = product * (n % 10 ); n = n / 10 ; } return product; } // Function that returns true if n // is prime else returns false static boolean isPrime( int n) { // Corner cases if (n <= 1 ) return false ; if (n <= 3 ) return true ; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n; i = i + 6 ) if (n % i == 0 || n % (i + 2 ) == 0 ) return false ; return true ; } // Function to return the smallest // prime number greater than N static int nextPrime( int N) { // Base case if (N <= 1 ) return 2 ; int prime = N; boolean found = false ; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true ; } return prime; } // Function to check Pointer-Prime numbers static boolean isPointerPrime( int n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true ; else return false ; } // Driver Code public static void main(String[] args) { // Given Number N int N = 23 ; // Function Call if (isPointerPrime(N)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by Shubham Prakash |
Python3
# Python3 implementation for the above approach def digProduct(n): product = 1 while (n ! = 0 ): product = product * (n % 10 ) n = int (n / 10 ) return product # Function that returns true if n # is prime else returns false def isPrime(n): # Corner cases if (n < = 1 ): return False if (n < = 3 ): return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 = = 0 or n % 3 = = 0 ): return False i = 5 while (i * i < = n): if (n % i = = 0 or n % (i + 2 ) = = 0 ): return False i = i + 6 return True # Function to return the smallest prime # number greater than N def nextPrime(N): # Base case if (N < = 1 ): return 2 ; prime = N found = False # Loop continuously until isPrime # returns true for a number greater # than n while ( not found): prime = prime + 1 if (isPrime(prime)): found = True return prime # Function to check Pointer-Prime numbers def isPointerPrime(n): if (isPrime(n) and (n + digProduct(n) = = nextPrime(n))): return True else : return False # Driver Code if __name__ = = "__main__" : # Given number N N = 23 # Function call if (isPointerPrime(N)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by adityakumar27200 |
C#
// C# program for above approach using System; class GFG{ // Function to find the product of // digits of a number N static int digProduct( int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function that returns true if n // is prime else returns false static bool isPrime( int n) { // Corner cases if (n <= 1) return false ; if (n <= 3) return true ; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false ; for ( int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false ; return true ; } // Function to return the smallest // prime number greater than N static int nextPrime( int N) { // Base case if (N <= 1) return 2; int prime = N; bool found = false ; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true ; } return prime; } // Function to check Pointer-Prime numbers static bool isPointerPrime( int n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true ; else return false ; } // Driver Code public static void Main() { // Given Number N int N = 23; // Function Call if (isPointerPrime(N)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by Code_Mech |
Javascript
<script> // Javascript implementation for the // above approach // Function to find the product of // digits of a number N function digProduct(n) { var product = 1; while (n != 0) { product = product * (n % 10); n = parseInt(n / 10); } return product; } // Function that returns true if n // is prime else returns false function isPrime(n) { // Corner cases if (n <= 1) return false ; if (n <= 3) return true ; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false ; for ( var i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false ; return true ; } // Function to return the smallest // prime number greater than N function nextPrime(N) { // Base case if (N <= 1) return 2; var prime = N; var found = false ; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true ; } return prime; } // Function to check Pointer-Prime numbers function isPointerPrime(n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true ; else return false ; } // Driver Code // Given Number N var N = 23; // Function Call if (isPointerPrime(N)) document.write( "Yes" ); else document.write( "No" ); </script> |
Yes
Time Complexity: O(n).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!