In this guide, we will explore Emirp Numbers in C++ along with the methodology and illustrations.
What is the Emirp Number?
An emirp number is defined as a prime number that is not a palindrome and still remains prime when its digits are reversed. When a prime number is formed by reversing the digits of another prime number, it is referred to as an emirp number, which essentially spells "prime" backwards. Emirps specifically exclude single-digit primes such as 7, 107, 113, 149, and 157, as well as palindromic primes like 151 or 787; instead, they are transformed into new prime numbers by flipping them.
Synopsis:
If a number is a prime number and its reverse is also a prime number, it is known as an emirp number.
As an example:
- When the digit is 113, it qualifies as a prime number; the reverse, 311, also stands as a prime number. Consequently, 113 is identified as an emirp number.
- In the case of 23, it represents a prime number, whereas its reverse, 32, does not. Therefore, 23 does not fall under the category of an emirp number.
The individual supplies an integer to input into this software. The specified number is initially checked for being a prime number. In case the number is prime, the reciprocal is calculated, and its primality is confirmed. An emirp number contains an emirp if its reverse is also a prime number; if not, it does not.
Algorithm:
Input: A number.
Determining if a given 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's consider a C++ program to determine if a specified 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 program mentioned above is O(x+y+p) where 'y' represents the input provided as a numerical value, 'x' denotes the reverse of the input numerical value, and 'p' signifies the count of digits in the input number.
However, for further efficiency, we can identify prime numbers by searching from 2 up to the square root of n. Given that n may have a maximum number of factors, it can be stated that the most optimized time complexity could be O(square root of n).
Space Complexity:
The program's space complexity is O(1) as it requires a consistent amount of memory space for execution.