指针素数是素数p ,因此可以通过将p的数字乘积从p获得p之后的下一个素数。
一些Pointer素数是:
23, 61, 1123, 1231, 1321, 2111, 2131, 11261….
检查N是否是指针素数
给定一个数字N ,任务是检查N是否为Pointer-Prime Number 。如果N是指针素数,则打印“是”,否则打印“否” 。
例子:
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
方法:
- 求N的数字的乘积
- 然后,找到N的下一个质数
- 现在,如果N是质数,并且N +的N +的乘积等于N的下一个质数,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ implementation for the
// above approach
#include
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
输出:
Yes
时间复杂度: O(n)
参考:http://oeis.org/A089823