Emirp号码(素数向后拼写)是一个素数,当其十进制数字反转时会产生不同的素数。该定义不包括相关的回文素数。
例子 :
Input : n = 13
Output : 13 is Emirp!
Explanation :
13 and 31 are both prime numbers.
Thus, 13 is an Emirp number.
Input : n = 27
Output : 27 is not Emirp.
目的:输入一个数字,然后查找该数字是否是一个emirp数字。
方法:输入一个数字,首先检查其是否为质数。如果该数字是质数,则我们找到原始数字的反数,然后检查该反向数是否为质数。如果反转的号码也是质数,则原始号码是Emirp号码,否则不是。
下面是上述方法的实现:
C++
// C++ program to check if given
// number is Emirp or not.
#include
using namespace std;
// Returns true if n is prime.
// Else false.
bool isPrime(int n)
{
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
// Function will check whether number
// is Emirp or not
bool isEmirp(int n)
{
// Check if n is prime
if (isPrime(n) == false)
return false;
// Find reverse of n
int rev = 0;
while (n != 0) {
int d = n % 10;
rev = rev * 10 + d;
n /= 10;
}
// If both Original and Reverse are Prime,
// then it is an Emirp number
return isPrime(rev);
}
// Driver code
int main()
{
int n = 13; // Input number
if (isEmirp(n) == true)
cout << "Yes";
else
cout << "No";
}
// This code is contributed by Anant Agarwal.
Java
// Java program to check if given number is
// Emirp or not.
import java.io.*;
class Emirp {
// Returns true if n is prime. Else
// false.
public static boolean isPrime(int n)
{
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
// Function will check whether number
// is Emirp or not
public static boolean isEmirp(int n)
{
// Check if n is prime
if (isPrime(n) == false)
return false;
// Find reverse of n
int rev = 0;
while (n != 0) {
int d = n % 10;
rev = rev * 10 + d;
n /= 10;
}
// If both Original and Reverse are Prime,
// then it is an Emirp number
return isPrime(rev);
}
// Driver Function
public static void main(String args[]) throws IOException
{
int n = 13; // Input number
if (isEmirp(n) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
Python3
# Python3 code to check if
# given number is Emirp or not.
# Returns true if n is prime.
# Else false.
def isPrime( n ):
# Corner case
if n <= 1:
return False
# Check from 2 to n-1
for i in range(2, n):
if n % i == 0:
return False
return True
# Function will check whether
# number is Emirp or not
def isEmirp( n):
# Check if n is prime
n = int(n)
if isPrime(n) == False:
return False
# Find reverse of n
rev = 0
while n != 0:
d = n % 10
rev = rev * 10 + d
n = int(n / 10)
# If both Original and Reverse
# are Prime, then it is an
# Emirp number
return isPrime(rev)
# Driver Function
n = 13 # Input number
if isEmirp(n):
print("Yes")
else:
print("No")
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to check if given
// number is Emirp or not.
using System;
class Emirp {
// Returns true if n is prime
// Else false.
public static bool isPrime(int n)
{
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
// Function will check whether number
// is Emirp or not
public static bool isEmirp(int n)
{
// Check if n is prime
if (isPrime(n) == false)
return false;
// Find reverse of n
int rev = 0;
while (n != 0) {
int d = n % 10;
rev = rev * 10 + d;
n /= 10;
}
// If both Original and Reverse are Prime,
// then it is an Emirp number
return isPrime(rev);
}
// Driver Function
public static void Main()
{
int n = 13; // Input number
if (isEmirp(n) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
Yes