Skip to content
Related Articles

Related Articles

Check whether a number is Emirpimes or not
  • Difficulty Level : Easy
  • Last Updated : 12 Dec, 2018

Given a number ‘n’, check whether it is an emirpimes or not.
An emirpimes(“semiprime” when spelled backwards) derives its definition from the way it is spelt. So, an emirpimes is a number that is a semiprime (product of two prime numbers) itself, and the reversal of its digits gives another new number, which too is a semi prime. Hence, by definition we can conclude that none of the palindrome numbers can be emirpimes, as the reversal of their digits does not give any new number, but forms the same number again.

Examples :

Input : 15
Output : Yes
Explanation : 15 is itself a semi prime number, since it is a product of two prime numbers 3 and 5. The reversal of its digits gives a new number 51, which too is a semi prime, it being the product of two prime numbers, viz., 3 and 17

Input : 49
Output : Yes
Explanation : 49 is itself a semi prime number, since it is a product of two prime numbers(not necessarily distinct) 7 and 7. The reversal of its digits gives a new number 94, which too is a semi prime, it being the product of two prime numbers, viz., 2 and 47

Input : 25
Output : No
Explanation : 25 is itself a semi prime number, since it is a product of two prime numbers(not necessarily distinct) 5 and 5. The reversal of its digits gives a new number 52, which is not a semi prime, it being the product of three and not two prime numbers, viz., 2, 2 and 13



Approach :

  1. First check whether the entered number is semi prime, itself.
  2. If yes, form a number by reversing its digits.
  3. Now, compare this number with the initially entered number to ascertain if the number is palindrome or not.
  4. If the number is not a palindrome, check whether this new number is also semi prime or not.
  5. If yes, then the initially entered number is reported to be an emirpimes.

C++




// CPP code to check whether
// a number is Emirpimes or not
#include <bits/stdc++.h>
using namespace std;
  
// Checking whether a number
// is semi-prime or not
int checkSemiprime(int num)
{
    int cnt = 0;
    for (int i = 2; cnt < 2 &&
                    i * i <= num; ++i)
    {
        while (num % i == 0)
        {
            num /= i;
  
            // Increment count of
            // prime numbers
            ++cnt;
        }
    }
  
    // If number is still greater than 1, after
    // exiting the for loop add it to the count
    // variable as it indicates the number is
    // a prime number
    if (num > 1)
        ++cnt;
  
    // Return '1' if count is 
    // equal to '2' else return '0'
    return cnt == 2;
}
  
// Checking whether a number
// is emirpimes or not
bool isEmirpimes(int n)
{
    // Number itself is not semiprime.
    if (checkSemiprime(n) == false)
        return false;
  
    // Finding reverse of n.
    int r = 0;
    for (int t=n; t!=0; t=t/n)
        r = r * 10 + t % 10;
  
    // The definition of emirpimes excludes
    // palindromes, hence we do not check
    // further, if the number entered is a
    // palindrome
    if (r == n)
        return false;
  
    // Checking whether the reverse of the
    // semi prime number entered is also
    // a semi prime number or not
    return (checkSemiprime(r));
}
  
// Driver Code
int main()
{
    int n = 15;
    if (isEmirpimes(n))
    cout << "Yes";
    else
        cout << "No";
    return 0;
}

Java




// Java code to check whether a
// number is Emirpimes or not
import java.io.*;
  
class GFG 
{
      
    // Checking whether a number
    // is semi-prime or not
    static boolean checkSemiprime(int num)
    {
        int cnt = 0;
        for (int i = 2; cnt < 2 && 
                        i * i <= num; ++i)
        {
            while (num % i == 0)
            {
                num /= i;
      
                // Increment count of
                // prime numbers
                ++cnt;
            }
        }
      
        // If number is still greater than 1,
        // after exiting the for loop add it 
        // to the count variable as it indicates
        // the number is a prime number
        if (num > 1)
            ++cnt;
      
        // Return '1' if count is equal
        // to '2' else return '0'
        return cnt == 2;
    }
      
    // Checking whether a number
    // is emirpimes or not
    static boolean isEmirpimes(int n)
    {
        // Number itself is not semiprime.
        if (checkSemiprime(n) == false)
            return false;
      
        // Finding reverse of n.
        int r = 0;
        for (int t = n; t != 0; t = t / n)
            r = r * 10 + t % 10;
      
        // The definition of emirpimes excludes
        // palindromes, hence we do not check
        // further, if the number entered is a
        // palindrome
        if (r == n)
            return false;
      
        // Checking whether the reverse of the
        // semi prime number entered is also
        // a semi prime number or not
        return (checkSemiprime(r));
    }
  
    // Driver Code
    public static void main (String[] args)
    {
        int n = 15;
        if (isEmirpimes(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by Ajit.

Python3




# Python3 code to check whether 
# a number is Emirpimesor not 
  
# Checking whether a number 
# is semi-prime or not 
def checkSemiprime(num): 
  
    cnt = 0;
    i = 2;
    while (cnt < 2 and (i * i) <= num): 
      
        while (num % i == 0):
            num /= i; 
  
            # Increment count of 
            # prime numbers 
            cnt += 1;
        i += 1;
  
    # If number is still greater than 1, 
    # after exiting the add it to the 
    # count variable as it indicates 
    # the number is a prime number 
    if (num > 1): 
        cnt += 1
  
    # Return '1' if count is equal 
    # to '2' else return '0' 
    return cnt == 2
  
# Checking whether a number 
# is emirpimes or not 
def isEmirpimes(n): 
      
    # Number itself is not semiprime. 
    if (checkSemiprime(n) == False): 
        return False
  
    # Finding reverse of n. 
    r = 0;
    t = n;
    while (t != 0): 
        r = r * 10 + t % 10;
        t = t / n;
  
    # The definition of emirpimes excludes 
    # palindromes, hence we do not check 
    # further, if the number entered 
    # is a palindrome 
    if (r == n): 
        return false; 
  
    # Checking whether the reverse of the 
    # semi prime number entered is also 
    # a semi prime number or not 
    return (checkSemiprime(r)); 
  
# Driver Code 
n = 15
if (isEmirpimes(n)): 
    print("No"); 
else:
    print("Yes"); 
  
# This code is contributed by mits

C#




// C# code to check whether a
// number is Emirpimes or not
using System;
  
class GFG
{
      
    // Checking whether a number
    // is semi-prime or not
    static bool checkSemiprime(int num)
    {
        int cnt = 0;
        for (int i = 2; cnt < 2 && 
                        i * i <= num; ++i)
        {
            while (num % i == 0)
            {
                num /= i;
      
                // Increment count of
                // prime numbers
                ++cnt;
            }
        }
      
        // If number is still greater than 1, 
        // after exiting the for loop add it 
        // to the count variable as it 
        // indicates the number is a prime number
        if (num > 1)
            ++cnt;
      
        // Return '1' if count is equal
        // to '2' else return '0'
        return cnt == 2;
    }
      
    // Checking whether a number
    // is emirpimes or not
    static bool isEmirpimes(int n)
    {
        // Number itself is not semiprime.
        if (checkSemiprime(n) == false)
            return false;
      
        // Finding reverse of n.
        int r = 0;
        for (int t = n; t != 0; t = t / n)
            r = r * 10 + t % 10;
      
        // The definition of emirpimes excludes
        // palindromes, hence we do not check
        // further, if the number entered is a
        // palindrome
        if (r == n)
            return false;
      
        // Checking whether the reverse of the
        // semi prime number entered is also
        // a semi prime number or not
        return (checkSemiprime(r));
    }
  
    // Driver Code
    public static void Main ()
    {
        int n = 15;
        if (isEmirpimes(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by anuj_67.

PHP




<?php
// PHP code to check whether 
// a number is Emirpimesor not
  
// Checking whether a number
// is semi-prime or not
function checkSemiprime($num)
{
    $cnt = 0;
    for ($i = 2; $cnt < 2 && 
         $i * $i <= $num; ++$i)
    {
        while ($num % $i == 0)
        {
            $num /= $i;
  
            // Increment count of
            // prime numbers
            ++$cnt;
        }
    }
  
    // If number is still greater
    // than 1, after exiting the
    // for loop add it to the 
    // count variable as it 
    // indicates the number is a
    // prime number
    if ($num > 1)
        ++$cnt;
  
    // Return '1' if count
    // is equal to '2' else
    // return '0'
    return $cnt == 2;
}
  
// Checking whether a number
// is emirpimes or not
function isEmirpimes($n)
{
      
    // Number itself is
    // not semiprime.
    if (checkSemiprime($n) == false)
        return false;
  
    // Finding reverse
    // of n.
    $r = 0;
    for ($t = $n; $t != 0; $t = $t / $n)
         $r = $r * 10 + $t % 10;
  
    // The definition of emirpimes  
    // excludes palindromes,hence  
    // we do not check further, 
    // if the number entered 
    // is a palindrome
    if ($r == $n)
        return false;
  
    // Checking whether the 
    // reverse of the
    // semi prime number 
    // entered is also
    // a semi prime number 
    // or not
    return (checkSemiprime($r));
}
  
    // Driver Code
    $n = 15;
    if (isEmirpimes($n))
      
    echo "No";
    else
    echo "Yes";
  
// This code is contributed by Ajit.
?>


Output :
Yes

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :