In this article, we will discuss the Emirp Numbers in C++ with its process and examples.
What is the Emirp Number?
An emirp number is a number that is a prime number but not a palindrome number, and it remains a prime number even if the digits of the number are reversed. A prime number that is generated when the digits of the number are reversed is called an emirp number , which is the word " prime" that is spelled backward. Emirps don't contain 1-digit primes like 7, 107, 113, 149, and 157, or palindromic primes like 151 or 787; instead, flip them to create a new prime number.
Synopsis:
If a number is prime and its reverse is also prime, it is referred to as an emirp number .
As an illustration:
- If the number is 113 , it is a prime number; 311 , the number's opposite , is likewise a prime number. Thus, 113 is an emirp number as a result.
- If the number is 23, it is a prime number, 32 the number opposite, is not a prime number. Thus 23 is not an emirp number.
The user provides a number to be entered into this program. The given integer is first examined for primeness. If the number is prime, its inverse is computed and its prime value is verified. An emirp number has an emirp if its reverse is likewise a prime number; otherwise, it does not.
Algorithm:
Input: A number.
Output: Whether the number is an emirp number or not.
Process:
Step 1: [In the first step, we will take the input]
Read x [the number to be checked for Emirp or not emirp]
Step 2: [In this step, we will check for emirp number]
Set y < 0
Set c < 0
Set tmp < - x [Check the original number is a prime number or not]
For i = 1 to x repeat
If x mod i = 0
Then
Set c < - c + 1
[End of 'if' condition]
[End of 'for' loop]
[If the number is prime, the reverse should be checked]
If c = 2
[Reverse the number]
While tmp>0
repeat
Set y< - y * 10 + (tmp mod 10)
[End of 'while' loop]
Set c <- 0
For i = 1 to y repeat
If y mod i = 0 then
Set c < - c + 1
[End of 'if' condition]
[End of 'for' loop]
[Checking for prime number]
If c = 2 then
Print "A number is an emirp number"
Else
Print "It is not an emirp number"
[End of 'if-else']
[End of 'if']
Else
Print "It is not an emirp number"
[End of 'else']
Step 3: Stop the Process.
Example:
Let us take a C++ program to check whether a given number is an emirp number or not:
#include<iostream>
using namespace std;
class Emirpnum{
private:
int y,x;
public:
void accept();
void emirp();
};
void Emirpnum:: accept()
{
cout<<"Enter the number: ";
cin>>y;
}
//function to check for emirp number
void Emirpnum::emirp()
{
int c = 0, tmp, i, n;
tmp = y;
x= 0;
//checking if the original number is prime or not
for ( i = 1; i <= y ;i++)
{
if(n % i ==0)
c ++;
}
if (c==2)
{
//reversing the number
while(tmp>0)
{
x=x* 10+( tmp %10) ;
tmp = tmp / 10;
}
//checking if the reverse of the number is also a prime number or not
c = 0;
for ( i = 1; i <= x ;i++)
{
if( x% i ==0)
c++;
}
//checking for prime number
if(c = 2)
cout<<"The number is an emirp number";
else
cout<<"The number is not an emirp number";
}
else
cout<<"The number is not an emirp number";
}
//main function
int main()
{
//creating an object
Emirpnum z;
//calling the function to check for emirp number
z.accept();
z.emirp();
}
Output:
Time Complexity:
The time complexity of the above program is O(x+y+p) where 'y' is the input, which is given as a number, 'x' is the reverse of the given input number, and 'p' is the number of digits of the given number.
However, for more optimization, we can find prime numbers by looking from 2 to the root of n. Since n can have a maximum number of factors, we can say that the most optimized time complexity can be O(root of n) .
Space Complexity:
The space complexity of the this program is O(1) because it takes a constant number of memory spaces to run the program.