- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Check whether a number is Emirpimes or not

The problem statement includes checking whether a number is Emirprimes or not, where the positive integer N will be the user input.

**An Emirpimes** number is a semiprime number whose when digits are being reversed, gives a new number which too is a semiprime number. A semiprime number is a number which is the product of two prime numbers which can be either distinct or the same.

In simple words, for a number N to be semiprime it should be of the form N=a*b, where a and b are prime numbers. They can be equal.

In this problem, we will be given a positive integer N in the input and our task will be to check if the given number is an Emirpimes or not.

**Example**

Let’s understand the problem with the below examples.

INPUT : N=39 OUTPUT : Yes

**Explanation** − The number in the input is 39. For a number to be an emirprimes, firstly the number itself should be a semiprime number. Since 39 can be written as 13*3 and both are the prime numbers, therefore it is a semiprime number.

The number itself and the number formed by the reverse of its digits, both should be distinct semiprime numbers to be an emirpimes.

The reverse of 39 is 93 which is also a semiprime as it can be written as a product of 3 and 31 which are prime numbers and both the numbers are distinct therefore 39 is an emirpimes.

INPUT : N=14 OUTPUT : No

**Explanation** − The number 14 is a semiprime as it can be expressed as product of 7 and 2 but when its digits are being reversed it makes a new number i.e. 41 which is not a semiprime as it is already a prime number, thus it can’t be expressed as product of two prime numbers. Therefore, 14 is not an emirpimes.

INPUT : N=22 OUTPUT : No

**Explanation** − The given number 22 is the semiprimes as it is a product of two prime numbers i.e. 11 and 2. The number formed by reversing its digits is 22 only. Since it doesn’t gives a new number, it is not an emirpimes.

Let’s understand the algorithm in order to check if the number is a emirpimes or not.

## Algorithm

We will simply check if the given number is a semiprime or not by counting the number of prime factors of the number. If the number of prime factors of the number is 2, then the number will be the semiprime as the number must be a product of two prime numbers to be a semiprime number.

If the number is a semiprime then we will calculate the new number by reversing the digits of the number and then check again if it is a semiprime or not. If it is a semiprime number, then the given number is emirpimes according to the condition for a number to be a emirpimes.

Let’s see the implementation of the algorithm in our approach in order to solve the problem of checking if the number is emirpimes or not in C++.

## Approach

The steps to follow to implement the algorithm in our approach in C++ −

We will make a function to check if the number is a semiprime number or not. In the function, we will initialise a variable to count the number of prime factors of the number N.

We will iterate in a for loop from i=2 to i<=sqrt(N) and check if N is divisible by i. If i divides N, then iterate in a nested while loop until i divides N and keep updating N and meanwhile increase count of prime factors by 1 at every iteration.

Now, check if N is greater than 1 for the case when N is a prime number greater than 1 which cannot become 1 with the above operation, so we will increase the count by 1.

If the count of prime factors is equal to 1, then the number is a semiprime number.

We will reverse the digits of N if N is a semiprime number and if N is not a semiprime number, we will return false as it can’t be an emirpimes.

Then check for the reversed number of N if it is equal to N as the number should be a distinct semiprime number to be an emirpimes. If it is a distinct number, then we will check if the number is a semiprime or not using the same function.

If the function returns true, the number is an emirpimes.

## Example

The C++ code for the approach −

//C++ code to check if the given number is an emirpimes or not #include <bits/stdc++.h> using namespace std; //function to check if the number is a semiprime or not bool semiprime(int N){ int a=0; //to store the count of number of prime factors //iterating in a for loop to check for every prime factor of N for(int i=2;i<=sqrt(N);i++){ if(N%i==0){ //keep updating N and increase count by 1 every time i divides N while(N%i==0){ N = N/i; a++; } } } //for the case when N is greater than 1 and is a prime number if(N>1){ a++; } if(a==2){ //if N has two prime factors, it is a semiprime return true; } else { return false; } } //function to check if the given number is an emirpimes or not bool emirpimes(int N){ //if N is a semiprime if(semiprime(N)==true){ int rev=0; //to store the number with reverse digits int num=N; //reverse the digits of N and store in rev while(num>0){ rev = rev*10 + num%10; num = num/10; } //if both the numbers are equal then return false if(N==rev){ return false; } //the number formed by reversing the digits of N should also be semiprime //for N to be emirpimes if(semiprime(rev)){ return true; } } //if N is not a semiprime then return false as it can't be an emirpimes return false; } int main() { int N; //for taking the input N=122; //calling the function if(emirpimes(N)){ cout<<N<<" is an emirpimes"<<endl; } else { cout<<N<<" is not an emirpimes"<<endl; } return 0; }

## Output

122 is an emirpimes

**Time Complexity : O(sqrt(N))**, time taken to check if the number is a semiprime or not.

**Space Complexity : O(1)**, because we didn’t take any extra space.

## Conclusion

The concepts of an emirpimes number and the conditions for a number to be an emirpimes was discussed in the article. We implement the conditions in our approach in C++ to check for any number if it is an emirpimes or not.

I hope you understand the concept of emirpimes number after reading this article.