Pointer-Primes

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 Nint 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 falsebool 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 Nint 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 numbersbool isPointerPrime(int n){ if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true; else return false;}// Driver Codeint main(){ // Given Number N int N = 23; // Function Call if (isPointerPrime(N)) cout << "Yes"; else cout << "No"; return 0;} |
Java
// Java program for above approachclass GFG{ // Function to find the product of// digits of a number Nstatic 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 falsestatic 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 Nstatic 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 numbersstatic 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 Codeif __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 approachusing System;class GFG{ // Function to find the product of// digits of a number Nstatic 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 falsestatic 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 Nstatic 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 numbersstatic 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 Nfunction 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 falsefunction 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 Nfunction 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 numbersfunction isPointerPrime(n){ if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true; else return false;}// Driver Code// Given Number Nvar N = 23;// Function Callif (isPointerPrime(N)) document.write( "Yes");else document.write( "No");</script> |
Output:
Yes
Time Complexity: O(n).
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



