Check whether a number is Emirpimes or not
Last Updated :
05 May, 2021
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 :
- First check whether the entered number is semi prime, itself.
- If yes, form a number by reversing its digits.
- Now, compare this number with the initially entered number to ascertain if the number is palindrome or not.
- If the number is not a palindrome, check whether this new number is also semi prime or not.
- If yes, then the initially entered number is reported to be an emirpimes.
C++
#include <bits/stdc++.h>
using namespace std;
int checkSemiprime( int num)
{
int cnt = 0;
for ( int i = 2; cnt < 2 &&
i * i <= num; ++i)
{
while (num % i == 0)
{
num /= i;
++cnt;
}
}
if (num > 1)
++cnt;
return cnt == 2;
}
bool isEmirpimes( int n)
{
if (checkSemiprime(n) == false )
return false ;
int r = 0;
for ( int t=n; t!=0; t=t/n)
r = r * 10 + t % 10;
if (r == n)
return false ;
return (checkSemiprime(r));
}
int main()
{
int n = 15;
if (isEmirpimes(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean checkSemiprime( int num)
{
int cnt = 0 ;
for ( int i = 2 ; cnt < 2 &&
i * i <= num; ++i)
{
while (num % i == 0 )
{
num /= i;
++cnt;
}
}
if (num > 1 )
++cnt;
return cnt == 2 ;
}
static boolean isEmirpimes( int n)
{
if (checkSemiprime(n) == false )
return false ;
int r = 0 ;
for ( int t = n; t != 0 ; t = t / n)
r = r * 10 + t % 10 ;
if (r == n)
return false ;
return (checkSemiprime(r));
}
public static void main (String[] args)
{
int n = 15 ;
if (isEmirpimes(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkSemiprime(num):
cnt = 0 ;
i = 2 ;
while (cnt < 2 and (i * i) < = num):
while (num % i = = 0 ):
num / = i;
cnt + = 1 ;
i + = 1 ;
if (num > 1 ):
cnt + = 1 ;
return cnt = = 2 ;
def isEmirpimes(n):
if (checkSemiprime(n) = = False ):
return False ;
r = 0 ;
t = n;
while (t ! = 0 ):
r = r * 10 + t % 10 ;
t = t / n;
if (r = = n):
return false;
return (checkSemiprime(r));
n = 15 ;
if (isEmirpimes(n)):
print ( "No" );
else :
print ( "Yes" );
|
C#
using System;
class GFG
{
static bool checkSemiprime( int num)
{
int cnt = 0;
for ( int i = 2; cnt < 2 &&
i * i <= num; ++i)
{
while (num % i == 0)
{
num /= i;
++cnt;
}
}
if (num > 1)
++cnt;
return cnt == 2;
}
static bool isEmirpimes( int n)
{
if (checkSemiprime(n) == false )
return false ;
int r = 0;
for ( int t = n; t != 0; t = t / n)
r = r * 10 + t % 10;
if (r == n)
return false ;
return (checkSemiprime(r));
}
public static void Main ()
{
int n = 15;
if (isEmirpimes(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkSemiprime( $num )
{
$cnt = 0;
for ( $i = 2; $cnt < 2 &&
$i * $i <= $num ; ++ $i )
{
while ( $num % $i == 0)
{
$num /= $i ;
++ $cnt ;
}
}
if ( $num > 1)
++ $cnt ;
return $cnt == 2;
}
function isEmirpimes( $n )
{
if (checkSemiprime( $n ) == false)
return false;
$r = 0;
for ( $t = $n ; $t != 0; $t = $t / $n )
$r = $r * 10 + $t % 10;
if ( $r == $n )
return false;
return (checkSemiprime( $r ));
}
$n = 15;
if (isEmirpimes( $n ))
echo "No" ;
else
echo "Yes" ;
?>
|
Javascript
<script>
function checkSemiprime(num)
{
let cnt = 0;
for (let i = 2; cnt < 2 && i * i <= num; ++i)
{
while (num % i == 0)
{
num = parseInt(num / i, 10);
++cnt;
}
}
if (num > 1)
++cnt;
return cnt == 2;
}
function isEmirpimes(n)
{
if (checkSemiprime(n) == false )
return false ;
let r = 0;
for (let t = n;
t != 0;
t = parseInt(t / n, 10))
r = r * 10 + t % 10;
if (r == n)
return false ;
return (checkSemiprime(r));
}
let n = 15;
if (isEmirpimes(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
Yes
Share your thoughts in the comments
Please Login to comment...