Check if a number can be expressed as product of a prime and a composite number
Given a number N, the task is to check if N can be represented as the product of a prime and a composite number or not. If it can, then print Yes, otherwise No.
Examples:
Input: N = 52
Output: Yes
Explanation: 52 can be represented as the multiplication of 4 and 13, where 4 is a composite and 13 is a prime number.
Input: N = 49
Output: No
Approach: This problem can be solved with the help of the Sieve of Eratosthenes algorithm. Now, to solve this problem, follow the below steps:
- Create a boolean array isPrime, where the ith element is true if it is a prime, otherwise it’s false.
- Find all prime numbers till N using sieve algorithm.
- Now run a loop for i=2 to i<N, and on each iteration:
- Check for these two conditions:
- If N is divisible by i.
- If i is a prime number and N/i isn’t or if i isn’t a prime number and N/i is.
- If both of the above conditions satisfy, return true.
- Otherwise, return false.
- Print the answer, according to the above observation.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes( int N, bool isPrime[])
{
isPrime[0] = isPrime[1] = false ;
for ( int i = 2; i <= N; i++)
isPrime[i] = true ;
for ( int p = 2; p * p <= N; p++) {
if (isPrime[p] == true ) {
for ( int i = p * 2; i <= N; i += p)
isPrime[i] = false ;
}
}
}
bool isRepresentable( int N)
{
bool isPrime[N + 1];
SieveOfEratosthenes(N, isPrime);
for ( int i = 2; i < N; i++) {
if (N % i == 0) {
if (N % i == 0
and (isPrime[i] and !isPrime[N / i])
or (!isPrime[i] and isPrime[N / i])) {
return true ;
}
}
}
return false ;
}
int main()
{
int N = 52;
if (isRepresentable(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void SieveOfEratosthenes( int N, boolean []isPrime)
{
isPrime[ 0 ] = isPrime[ 1 ] = false ;
for ( int i = 2 ; i <= N; i++)
isPrime[i] = true ;
for ( int p = 2 ; p * p <= N; p++) {
if (isPrime[p] == true ) {
for ( int i = p * 2 ; i <= N; i += p)
isPrime[i] = false ;
}
}
}
static boolean isRepresentable( int N)
{
boolean []isPrime = new boolean [N + 1 ];
SieveOfEratosthenes(N, isPrime);
for ( int i = 2 ; i < N; i++) {
if (N % i == 0 ) {
if (N % i == 0
&& (isPrime[i] && !isPrime[N / i])
|| (!isPrime[i] && isPrime[N / i])) {
return true ;
}
}
}
return false ;
}
public static void main(String arg[])
{
int N = 52 ;
if (isRepresentable(N)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
import math
def SieveOfEratosthenes(N, isPrime):
isPrime[ 0 ] = False
isPrime[ 1 ] = False
for i in range ( 2 , N + 1 ):
isPrime[i] = True
for p in range ( 2 , int (math.sqrt(N)) + 1 ):
if (isPrime[p] = = True ):
for i in range (p + 2 , N + 1 , p):
isPrime[i] = False
def isRepresentable(N):
isPrime = [ 0 for _ in range (N + 1 )]
SieveOfEratosthenes(N, isPrime)
for i in range ( 2 , N):
if (N % i = = 0 ):
if (N % i = = 0 and (isPrime[i] and not isPrime[N / / i]) or ( not isPrime[i] and isPrime[N / / i])):
return True
return False
if __name__ = = "__main__" :
N = 52
if (isRepresentable(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static void SieveOfEratosthenes( int N, bool []isPrime)
{
isPrime[0] = isPrime[1] = false ;
for ( int i = 2; i <= N; i++)
isPrime[i] = true ;
for ( int p = 2; p * p <= N; p++) {
if (isPrime[p] == true ) {
for ( int i = p * 2; i <= N; i += p)
isPrime[i] = false ;
}
}
}
static bool isRepresentable( int N)
{
bool []isPrime = new bool [N + 1];
SieveOfEratosthenes(N, isPrime);
for ( int i = 2; i < N; i++) {
if (N % i == 0) {
if (N % i == 0
&& (isPrime[i] && !isPrime[N / i])
|| (!isPrime[i] && isPrime[N / i])) {
return true ;
}
}
}
return false ;
}
public static void Main()
{
int N = 52;
if (isRepresentable(N)) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function SieveOfEratosthenes(N, isPrime)
{
isPrime[0] = isPrime[1] = false ;
for (let i = 2; i <= N; i++)
isPrime[i] = true ;
for (let p = 2; p * p <= N; p++) {
if (isPrime[p] == true ) {
for (let i = p * 2; i <= N; i += p)
isPrime[i] = false ;
}
}
}
function isRepresentable(N)
{
let isPrime = [];
SieveOfEratosthenes(N, isPrime);
for (let i = 2; i < N; i++) {
if (N % i == 0) {
if (N % i == 0
&& (isPrime[i] && !isPrime[N / i])
|| (!isPrime[i] && isPrime[N / i])) {
return true ;
}
}
}
return false ;
}
let N = 52;
if (isRepresentable(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time complexity: O(N*log(logN))
Auxiliary Space: O(N)
Another Approach
Below is the implementation of the above approach:
C++
#include <iostream>
#include <cmath>
bool isPrime( int n) {
if (n <= 1)
return false ;
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0)
return false ;
}
return true ;
}
bool isProductOfPrimeAndComposite( int N) {
if (N <= 3)
return false ;
for ( int i = 2; i <= sqrt (N); i++) {
if (N % i == 0) {
int factor1 = i;
int factor2 = N / i;
if (isPrime(factor1) && !isPrime(factor2))
return true ;
if (!isPrime(factor1) && isPrime(factor2))
return true ;
}
}
return false ;
}
int main() {
int N=52;
if (isProductOfPrimeAndComposite(N))
std::cout << "Yes" << std::endl;
else
std::cout << "No" << std::endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean isPrime( int n) {
if (n <= 1 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(n); i++) {
if (n % i == 0 )
return false ;
}
return true ;
}
static boolean isProductOfPrimeAndComposite( int N) {
if (N <= 3 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(N); i++) {
if (N % i == 0 ) {
int factor1 = i;
int factor2 = N / i;
if (isPrime(factor1) && !isPrime(factor2))
return true ;
if (!isPrime(factor1) && isPrime(factor2))
return true ;
}
}
return false ;
}
public static void main(String[] args) {
int N = 52 ;
if (isProductOfPrimeAndComposite(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import math
def is_prime(n):
if n < = 1 :
return False
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if n % i = = 0 :
return False
return True
def is_product_of_prime_and_composite(N):
if N < = 3 :
return False
for i in range ( 2 , int (math.sqrt(N)) + 1 ):
if N % i = = 0 :
factor1 = i
factor2 = N / / i
if is_prime(factor1) and not is_prime(factor2):
return True
if not is_prime(factor1) and is_prime(factor2):
return True
return False
if __name__ = = "__main__" :
N = 52
if is_product_of_prime_and_composite(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool IsPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
static bool IsProductOfPrimeAndComposite( int N)
{
if (N <= 3)
return false ;
for ( int i = 2; i <= Math.Sqrt(N); i++)
{
if (N % i == 0)
{
int factor1 = i;
int factor2 = N / i;
if (IsPrime(factor1) && !IsPrime(factor2))
return true ;
if (!IsPrime(factor1) && IsPrime(factor2))
return true ;
}
}
return false ;
}
static void Main( string [] args)
{
int N = 52;
if (IsProductOfPrimeAndComposite(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isPrime(n) {
if (n <= 1)
return false ;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0)
return false ;
}
return true ;
}
function isProductOfPrimeAndComposite(N) {
if (N <= 3)
return false ;
for (let i = 2; i <= Math.sqrt(N); i++) {
if (N % i === 0) {
let factor1 = i;
let factor2 = N / i;
if (isPrime(factor1) && !isPrime(factor2))
return true ;
if (!isPrime(factor1) && isPrime(factor2))
return true ;
}
}
return false ;
}
let N = 52;
if (isProductOfPrimeAndComposite(N))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Last Updated :
22 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...