-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emirp.java
52 lines (46 loc) · 1.14 KB
/
Emirp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//A number is called an emirp number if we get another prime number on reversing the number itself. In other words, an emirp number is a number that is prime forwards or backward. It is also known as twisted prime numbers.
// 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");
}
}