# Check if given number is Emirp Number or not

An Emirp Number (prime spelled backwards) is a prime number that results in a different prime when its decimal digits are reversed. This definition excludes the related palindromic primes.
Examples :

`Input : n = 13Output : 13 is Emirp!Explanation :13 and 31 are both prime numbers. Thus, 13 is an Emirp number.Input : n = 27Output : 27 is not Emirp.`

Objective: Input a number and find whether the number is an emirp number or not.

Approach: Input a number and firstly check if its a prime number or not. If the number is a prime number, then we find the reverse of the original number and check the reversed number for being prime or not. If the reversed number is also prime, then the original number is an Emirp Number otherwise it is not.
Below is the implementation of the above approach :.

## C++

 `// C++ program to check if given``// number is Emirp or not.``#include ``using` `namespace` `std;` `// Returns true if n is prime.``// Else false.``bool` `isPrime(``int` `n)``{``    ``// Corner case``    ``if` `(n <= 1)``        ``return` `false``;` `    ``// Check from 2 to n-1``    ``for` `(``int` `i = 2; i < n; i++)``        ``if` `(n % i == 0)``            ``return` `false``;` `    ``return` `true``;``}` `// Function will check whether number``// is Emirp or not``bool` `isEmirp(``int` `n)``{``    ``// Check if n is prime``    ``if` `(isPrime(n) == ``false``)``        ``return` `false``;` `    ``// Find reverse of n``    ``int` `rev = 0;``    ``while` `(n != 0) {``        ``int` `d = n % 10;``        ``rev = rev * 10 + d;``        ``n /= 10;``    ``}` `    ``// If both Original and Reverse are Prime,``    ``// then it is an Emirp number``    ``return` `isPrime(rev);``}` `// Driver code``int` `main()``{``    ``int` `n = 13; ``// Input number``    ``if` `(isEmirp(n) == ``true``)``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"``;``}` `// This code is contributed by Anant Agarwal.`

## Java

 `// Java program to check if given number is``// Emirp or not.``import` `java.io.*;``class` `Emirp {``    ``// Returns true if n is prime. Else``    ``// false.``    ``public` `static` `boolean` `isPrime(``int` `n)``    ``{``        ``// Corner case``        ``if` `(n <= ``1``)``            ``return` `false``;` `        ``// Check from 2 to n-1``        ``for` `(``int` `i = ``2``; i < n; i++)``            ``if` `(n % i == ``0``)``                ``return` `false``;` `        ``return` `true``;``    ``}` `    ``// Function will check whether number``    ``// is Emirp or not``    ``public` `static` `boolean` `isEmirp(``int` `n)``    ``{``        ``// Check if n is prime``        ``if` `(isPrime(n) == ``false``)``            ``return` `false``;` `        ``// Find reverse of n``        ``int` `rev = ``0``;``        ``while` `(n != ``0``) {``            ``int` `d = n % ``10``;``            ``rev = rev * ``10` `+ d;``            ``n /= ``10``;``        ``}` `        ``// If both Original and Reverse are Prime,``        ``// then it is an Emirp number``        ``return` `isPrime(rev);``    ``}` `    ``// Driver Function``    ``public` `static` `void` `main(String args[]) ``throws` `IOException``    ``{``        ``int` `n = ``13``; ``// Input number``        ``if` `(isEmirp(n) == ``true``)``            ``System.out.println(``"Yes"``);``        ``else``            ``System.out.println(``"No"``);``    ``}``}`

## Python3

 `# Python3 code to check if ``# given number is Emirp or not.` `# Returns true if n is prime. ``# Else false.``def` `isPrime( n ):``    ` `    ``# Corner case``    ``if` `n <``=` `1``:``        ``return` `False``    ` `    ``# Check from 2 to n-1``    ``for` `i ``in` `range``(``2``, n):``        ``if` `n ``%` `i ``=``=` `0``:``            ``return` `False``    ` `    ``return` `True` `# Function will check whether``# number is Emirp or not``def` `isEmirp( n):``    ` `    ``# Check if n is prime``    ``n ``=` `int``(n)``    ``if` `isPrime(n) ``=``=` `False``:``        ``return` `False``        ` `        ``# Find reverse of n``    ``rev ``=` `0``    ``while` `n !``=` `0``:``        ``d ``=` `n ``%` `10``        ``rev ``=` `rev ``*` `10` `+` `d``        ``n ``=` `int``(n ``/` `10``)``        ` `        ` `    ``# If both Original and Reverse ``    ``# are Prime, then it is an``    ``# Emirp number``    ``return` `isPrime(rev)` `# Driver Function``n ``=` `13` `# Input number``if` `isEmirp(n):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)``    ` `# This code is contributed by "Sharad_Bhardwaj".`

## C#

 `// C# program to check if given``// number is Emirp or not.``using` `System;` `class` `Emirp {``    ``// Returns true if n is prime``    ``// Else false.``    ``public` `static` `bool` `isPrime(``int` `n)``    ``{``        ``// Corner case``        ``if` `(n <= 1)``            ``return` `false``;` `        ``// Check from 2 to n-1``        ``for` `(``int` `i = 2; i < n; i++)``            ``if` `(n % i == 0)``                ``return` `false``;` `        ``return` `true``;``    ``}` `    ``// Function will check whether number``    ``// is Emirp or not``    ``public` `static` `bool` `isEmirp(``int` `n)``    ``{``        ``// Check if n is prime``        ``if` `(isPrime(n) == ``false``)``            ``return` `false``;` `        ``// Find reverse of n``        ``int` `rev = 0;``        ``while` `(n != 0) {``            ``int` `d = n % 10;``            ``rev = rev * 10 + d;``            ``n /= 10;``        ``}` `        ``// If both Original and Reverse are Prime,``        ``// then it is an Emirp number``        ``return` `isPrime(rev);``    ``}` `    ``// Driver Function``    ``public` `static` `void` `Main()``    ``{``        ``int` `n = 13; ``// Input number``        ``if` `(isEmirp(n) == ``true``)``            ``Console.WriteLine(``"Yes"``);``        ``else``            ``Console.WriteLine(``"No"``);``    ``}``}` `// This code is contributed by vt_m.`

## Javascript

 ``

## PHP

 ``

Output :

`Yes`

Time complexity: O(n)

Auxiliary Space:O(1)

#### Approach#2: Using recursion

Check if the given number is prime or not If it is prime, reverse the number and check if the reversed number is also prime or not If both the original and reversed numbers are prime, then the given number is an Emirp number

#### Algorithm

1. Define a function is_prime() to check if a number is prime or not
2. Define a function reverse_number() to reverse a given number
3. Define a function is_emirp() which takes a number as input
4. Check if the given number is prime or not using is_prime() function
5. If it is not prime, return “Not Emirp”
6. Reverse the given number using reverse_number() function
7. Check if the reversed number is prime using is_prime() function
8. If both the original and reversed numbers are prime, return “Emirp”, else return “Not Emirp”

## C++

 `#include ``#include ``#include ` `using` `namespace` `std;` `// Function to check if a number is prime``bool` `is_prime(``int` `num)``{``    ``if` `(num < 2) {``        ``return` `false``;``    ``}``    ``for` `(``int` `i = 2; i <= ``sqrt``(num); i++) {``        ``if` `(num % i == 0) {``            ``return` `false``;``        ``}``    ``}``    ``return` `true``;``}` `// Function to reverse a number``int` `reverse_number(``int` `num)``{``    ``string str_num = to_string(num);``    ``string rev_str_num = ``""``;``    ``for` `(``int` `i = str_num.length() - 1; i >= 0; i--) {``        ``rev_str_num += str_num[i];``    ``}``    ``return` `stoi(rev_str_num);``}` `// Function to check if a number is an emirp``string is_emirp(``int` `num)``{``    ``if` `(!is_prime(num)) {``        ``return` `"Not Emirp"``;``    ``}``    ``int` `rev_num = reverse_number(num);``    ``if` `(is_prime(rev_num) && num != rev_num) {``        ``return` `"Emirp"``;``    ``}``    ``else` `{``        ``return` `"Not Emirp"``;``    ``}``}` `int` `main()``{``    ``int` `num = 27;``    ``cout << is_emirp(num) << endl;``}`

## Java

 `import` `java.lang.Math;``import` `java.util.Scanner;` `public` `class` `Emirp {``    ``// Function to check if a number is prime``    ``public` `static` `boolean` `isPrime(``int` `num) {``        ``if` `(num < ``2``) {``            ``return` `false``;``        ``}``        ``for` `(``int` `i = ``2``; i <= Math.sqrt(num); i++) {``            ``if` `(num % i == ``0``) {``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}` `    ``// Function to reverse a number``    ``public` `static` `int` `reverseNumber(``int` `num) {``        ``String strNum = Integer.toString(num);``        ``String revStrNum = ``""``;``        ``for` `(``int` `i = strNum.length() - ``1``; i >= ``0``; i--) {``            ``revStrNum += strNum.charAt(i);``        ``}``        ``return` `Integer.parseInt(revStrNum);``    ``}` `    ``// Function to check if a number is an emirp``    ``public` `static` `String isEmirp(``int` `num) {``        ``if` `(!isPrime(num)) {``            ``return` `"Not Emirp"``;``        ``}``        ``int` `revNum = reverseNumber(num);``        ``if` `(isPrime(revNum) && num != revNum) {``            ``return` `"Emirp"``;``        ``} ``else` `{``            ``return` `"Not Emirp"``;``        ``}``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``int` `num = ``27``;``        ``System.out.println(isEmirp(num));``    ``}``}`

## Python3

 `def` `is_prime(num):``    ``if` `num < ``2``:``        ``return` `False``    ``for` `i ``in` `range``(``2``, ``int``(num``*``*``0.5``)``+``1``):``        ``if` `num ``%` `i ``=``=` `0``:``            ``return` `False``    ``return` `True`  `def` `reverse_number(num):``    ``return` `int``(``str``(num)[::``-``1``])`  `def` `is_emirp(num):``    ``if` `not` `is_prime(num):``        ``return` `"Not Emirp"``    ``rev_num ``=` `reverse_number(num)``    ``if` `is_prime(rev_num) ``and` `num !``=` `rev_num:``        ``return` `"Emirp"``    ``else``:``        ``return` `"Not Emirp"`  `num ``=` `27``print``(is_emirp(num))`

## C#

 `using` `System;` `namespace` `Emirp``{``    ``class` `GFG``    ``{``        ``// Function to check if a number is prime``        ``static` `bool` `IsPrime(``int` `num)``        ``{``            ``if` `(num < 2)``            ``{``                ``return` `false``;``            ``}``            ``for` `(``int` `i = 2; i <= Math.Sqrt(num); i++)``            ``{``                ``if` `(num % i == 0)``                ``{``                    ``return` `false``;``                ``}``            ``}``            ``return` `true``;``        ``}` `        ``// Function to reverse a number``        ``static` `int` `ReverseNumber(``int` `num)``        ``{``            ``string` `strNum = num.ToString();``            ``char``[] charArray = strNum.ToCharArray();``            ``Array.Reverse(charArray);``            ``string` `revStrNum = ``new` `string``(charArray);``            ``return` `int``.Parse(revStrNum);``        ``}` `        ``// Function to check if a number is an emirp``        ``static` `string` `IsEmirp(``int` `num)``        ``{``            ``if` `(!IsPrime(num))``            ``{``                ``return` `"Not Emirp"``;``            ``}``            ``int` `revNum = ReverseNumber(num);``            ``if` `(IsPrime(revNum) && num != revNum)``            ``{``                ``return` `"Emirp"``;``            ``}``            ``else``            ``{``                ``return` `"Not Emirp"``;``            ``}``        ``}` `        ``static` `void` `Main(``string``[] args)``        ``{``            ``int` `num = 27;``            ``Console.WriteLine(IsEmirp(num));``        ``}``    ``}``}`

## Javascript

 `function` `isPrime(num) {``  ``if` `(num < 2) {``    ``return` `false``;``  ``}``  ``for` `(let i = 2; i <= Math.sqrt(num); i++) {``    ``if` `(num % i === 0) {``      ``return` `false``;``    ``}``  ``}``  ``return` `true``;``}` `function` `reverseNumber(num) {``  ``return` `parseInt(num.toString().split(``''``).reverse().join(``''``));``}` `function` `isEmirp(num) {``  ``if` `(!isPrime(num)) {``    ``return` `"Not Emirp"``;``  ``}``  ``const revNum = reverseNumber(num);``  ``if` `(isPrime(revNum) && num !== revNum) {``    ``return` `"Emirp"``;``  ``} ``else` `{``    ``return` `"Not Emirp"``;``  ``}``}` `const num = 27;``console.log(isEmirp(num));`

Output
```Not Emirp

```

Time Complexity: O(sqrt(n)), n is the input number for which we are checking if it is an Emirp number or not.
Auxiliary Space: O(log n), n is the input number for which we are checking if it is an Emirp number or not.

Previous
Next