Emirp Number in Java
In this tutorial, we will learn about emirp numbers in Java. We will understand this using java programs.
An emirp number is a prime number that remains to be a prime number when its digits are reversed. It is also called twisted Prime numbers.
It is to be noted that palindrome prime numbers are not included int this.
For example,
13 - Its reverse is 31 which is also a prime number thus, 13 and 31 are emirp numbers.
79 - Its reverse is 97 which is also a prime number thus, 79 and 97 are emirp numbers.
113 - Its reverse is 311 which is also a prime number thus, 113 and 331 are emirp numbers.
Algorithm:
Let us see the steps followed by our Java code to calculate the emirp number in Java.
- The program defines a class called "EmirpNumber1".
- The program defines a function called "isPrime" which takes an integer "num" as input and returns a boolean value indicating whether "num" is prime or not. The function checks whether "num" is less than or equal to 1. If it is, the function returns false, because 1 is not considered a prime number. If "num" is greater than 1, the function checks whether any integer from 2 to num-1 divides "num" without leaving a remainder. If any integer does, the function returns false, because "num" is not prime. If none of the integers divide "num" without leaving a remainder, the function returns true, because "num" is prime.
- The program defines a function called "isEmirp" which takes an integer "num" as input and returns a boolean value indicating whether "num" is an emirp number or not. The function first checks whether "num" is prime or not using the "isPrime" function defined earlier. If "num" is not prime, the function returns false, because emirp numbers are prime. If "num" is prime, the function reverses the digits of "num" and stores the result in a variable called "rev". This is done by extracting the last digit of "num" using the modulo operator and adding it to "rev" multiplied by 10. The last digit is then removed from "num" by dividing it by 10. This process is repeated until "num" becomes zero. The function then calls the "isPrime" function again, this time with the reversed number "rev" as input, to check whether it is prime or not. If "rev" is prime, the function returns true, because "num" is an emirp number. If "rev" is not prime, the function returns false, because "num" is not an emirp number.
- The program defines a main function which is the entry point of the program. The main function prompts the user to enter a number to check if it is an emirp number or not. It then reads an integer from the user using a Scanner object. The main function then calls the "isEmirp" function with the entered number as input and checks whether the function returns true or false. If the function returns true, the main function prints the message "Yes, the entered number is an emirp number." If the function returns false, the main function prints the message "No, the entered number is not an emirp number."
Implementation:
Here's a Java program that checks if a given number is an emirp number:
File name - EmirpNumber1.java
import java.io.*;
import java.util.*;
public class EmirpNumber1
{
//function to check whether the entered number is prime or not
public static boolean isPrime(int num)
{
//base case
if (num <= 1)
return false;
//loop executes from 2 to num-1
for (int i = 2; i < num; i++)
if (num % i == 0)
//returns false if this condition is true
return false;
//returns true if this condition is false
return true;
}
//function to check whether the given number is emirp or not
public static boolean isEmirp(int num)
{
//checks if the given number is prime or not
if (isPrime(num) == false)
return false;
//variable to store the reverse of the number
int rev = 0;
//the while loop executes until the specified condition becomes false
while (num != 0)
{
//finds the last digit of the number (n)
int digit = num % 10;
//finds the reverse of the given number
rev = rev * 10 + digit;
//removes the last digit
num = num/10;
}
//calling the user-defined function that checks the reverse number is prime or not
return isPrime(rev);
}
//driver code
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number to check if it emirp or not: ");
//reading an integer from the user
int num=sc.nextInt();
if (isEmirp(num) == true)
System.out.println("Yes, the entered number is an emirp number.");
else
System.out.println("No, the entered number is not an emirp number.");
}
}
Output 1:
Output 2:
Complexity Analysis:
Time complexity: The time complexity of the "isPrime" function in the given code is O(sqrt(num)). The time complexity of the "isEmirp" function in the given code is O(log(num) + sqrt(num)). The time complexity of the main function is dominated by the time complexity of the "isEmirp" function.
Therefore, the overall time complexity of the program is O(log(num) + sqrt(num)) in the worst-case scenario where "num" is a large prime number.
Space complexity: The space complexity of the given code is O(1), or constant.