Pointer-Primes
Last Updated :
22 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int digProduct( int n)
{
int product = 1;
while (n != 0) {
product = product * (n % 10);
n = n / 10;
}
return product;
}
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 ;
}
int nextPrime( int N)
{
if (N <= 1)
return 2;
int prime = N;
bool found = false ;
while (!found) {
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
bool isPointerPrime( int n)
{
if (isPrime(n)
&& (n + digProduct(n) == nextPrime(n)))
return true ;
else
return false ;
}
int main()
{
int N = 23;
if (isPointerPrime(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static int digProduct( int n)
{
int product = 1 ;
while (n != 0 )
{
product = product * (n % 10 );
n = n / 10 ;
}
return product;
}
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
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 ;
}
static int nextPrime( int N)
{
if (N <= 1 )
return 2 ;
int prime = N;
boolean found = false ;
while (!found)
{
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
static boolean isPointerPrime( int n)
{
if (isPrime(n) &&
(n + digProduct(n) == nextPrime(n)))
return true ;
else
return false ;
}
public static void main(String[] args)
{
int N = 23 ;
if (isPointerPrime(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def digProduct(n):
product = 1
while (n ! = 0 ):
product = product * (n % 10 )
n = int (n / 10 )
return product
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
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
def nextPrime(N):
if (N < = 1 ):
return 2 ;
prime = N
found = False
while ( not found):
prime = prime + 1
if (isPrime(prime)):
found = True
return prime
def isPointerPrime(n):
if (isPrime(n) and
(n + digProduct(n) = = nextPrime(n))):
return True
else :
return False
if __name__ = = "__main__" :
N = 23
if (isPointerPrime(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int digProduct( int n)
{
int product = 1;
while (n != 0)
{
product = product * (n % 10);
n = n / 10;
}
return product;
}
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 ;
}
static int nextPrime( int N)
{
if (N <= 1)
return 2;
int prime = N;
bool found = false ;
while (!found)
{
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
static bool isPointerPrime( int n)
{
if (isPrime(n) &&
(n + digProduct(n) == nextPrime(n)))
return true ;
else
return false ;
}
public static void Main()
{
int N = 23;
if (isPointerPrime(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function digProduct(n)
{
var product = 1;
while (n != 0) {
product = product * (n % 10);
n = parseInt(n / 10);
}
return product;
}
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 nextPrime(N)
{
if (N <= 1)
return 2;
var prime = N;
var found = false ;
while (!found) {
prime++;
if (isPrime(prime))
found = true ;
}
return prime;
}
function isPointerPrime(n)
{
if (isPrime(n)
&& (n + digProduct(n) == nextPrime(n)))
return true ;
else
return false ;
}
var N = 23;
if (isPointerPrime(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n).
Share your thoughts in the comments
Please Login to comment...