Emirp Number in Java

In this blog we will learn about the concept of “Emirp Number in Java”, What is the Emirp Number in Java? How to find Emirp Number in Java. Let’s look further into the blog.

What is the Emirp Number in Java?

When we reverse a number, we get another prime number. An emirp number, in other words, is a number that is prime both forwards and backward. Twisted prime numbers are another name for it.

Java Program to check whether a given number is an Emirp number or not

Now, let’s proceed to check whether a given number is an Emirp number or not by Java Program:

import java.io.*;  
import java.util.*;  
public class EmirpNumberEx   
//function that checks the given number is prime or not  
public static boolean isPrime(int n)  
//base case  
if (n <= 1)  
return false;  
//loop executes from 2 to n-1  
for (int i = 2; i < n; i++)  
if (n % i == 0)  
//returns false if the condition returns true  
return false;  
//returns true if the condition returns false  
return true;  
//function that checks if the given number is emirp or not  
public static boolean isEmirp(int n)  
//checks if the given number is prime or not  
if (isPrime(n) == false)  
return false;  
//variable that stores the reverse of the number  
int reverse = 0;  
//the while loop executes until the specified condition becomes false  
while (n != 0)   
//finds the last digit of the number (n)  
int digit = n % 10;  
//finds the reverse of the given number  
reverse = reverse * 10 + digit;  
//removes the last digit  
n = n/10;  
//calling the user-defined function that checks the reverse number is prime or not  
return isPrime(reverse);  
//driver code  
public static void main(String args[])   
Scanner sc=new Scanner(System.in);  
System.out.print("Enter a number to check: ");  
//reading an integer from the user  
int n=sc.nextInt();  
if (isEmirp(n) == true)  
System.out.println("Yes, the given number is an emirp number.");  
System.out.println("No, the given number is not an emirp number.");  

Explanation of the code:

The program first defines a function named “isPrime” that takes an integer as input and checks whether it is a prime number or not. It iterates from 2 to n-1 and if the number is divisible by any other number, it returns false. If the loop completes without finding any divisors, it returns true.

Next, there is another function called “isEmirp” that takes an integer as input and checks if it is an Emirp number. It first checks if the number is prime using the “isPrime” function. If it is not prime, it returns false. It then proceeds to reverse the number by extracting the last digit, multiplying the reverse by 10, and adding the digit. This process continues until the original number becomes 0. Finally, it checks whether the reversed number is also prime using the “isPrime” function and returns the result.

In the main function, the program prompts the user to enter a number to check. It reads the input using the Scanner class and calls the “isEmirp” function to determine whether the number is an Emirp number or not. Depending on the result, it prints the appropriate message to the console.


Enter a number to check: 1583
Yes, the given number is an emirp number.

Also, learn about Spy Number in Java here!

Discussing possible optimizations to the Emirp number program

To optimize prime number generation, we can use the Sieve of Eratosthenes algorithm instead of checking divisibility by all numbers up to the square root of a number. This algorithm can efficiently generate prime numbers up to a given limit, reducing the number of  computations needed.

Caching prime numbers: Rather than recalculating prime numbers for each iteration, we can store them in a cache or an array for quick access. This can significantly improve the performance of the Emirp number program, especially for larger numbers. 

To make the Emirp number program faster, we can use multi-threading. This means running multiple calculations at the same time, which speeds up the computation of Emirp numbers.

Check out our blog on Prime Number in Java here!

Emirp Number in Java: Exploring additional features and functionalities

  • Modify the program to allow users to input a range and find all Emirp numbers within that range by checking each number in the range.
  • Allow users to customize the program by inputting parameters like the number of Emirp numbers to find, the starting point, or the search range. This makes the program more flexible and user-friendly.
  • Create a GUI for Emirp number program, enhancing user experience with input fields, execution buttons, and display area.

Introducing related topics like palindromic primes

Palindromic primes: Palindromic primes are prime and palindromic numbers, reading the same forwards and backward, similar to Emirp numbers.

Emirp twins: Emirp twins are pairs of Emirp numbers with fixed values, significant in number theory, and easily identified using existing Emirp number programs. such as (13, 31) or (17, 71). 

Other number patterns: Look at more fascinating prime-related notions and number patterns, such as twin primes, sexy primes, and Sophie Germain primes. Introduce these ideas succinctly and describe how they are related to or dissimilar from Emirp numbers.

Learn more about the Palindrome Program in Java here!

In conclusion, Emirp numbers are prime numbers that remain prime when their digits are reversed, making them fascinating and practical in mathematics and cryptography.

Throughout this blog, we explored how to implement Emirp numbers in Java, learning about prime numbers, reversing digits, and creating a step-by-step guide to identify them efficiently and accurately. Check out Newtum’s website and explore our online coding courses covering Java, Python, PHP, and more.

About The Author

Leave a Reply