GFG App
Open App
Browser
Continue

# 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:

1. Create a boolean array isPrime, where the ith element is true if it is a prime, otherwise it’s false.
2. Find all prime numbers till N using sieve algorithm.
3. 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.
4. Print the answer, according to the above observation.

Below is the implementation of the above approach.

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to generate all prime` `// numbers less than N` `void` `SieveOfEratosthenes(``int` `N, ``bool` `isPrime[])` `{` `    ``// Initialize all entries of boolean array` `    ``// as true. A value in isPrime[i] will finally` `    ``// be false if i is Not a prime, else true` `    ``// bool isPrime[N+1];` `    ``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] is not changed,` `        ``// then it is a prime` `        ``if` `(isPrime[p] == ``true``) {`   `            ``// Update all multiples of p` `            ``for` `(``int` `i = p * 2; i <= N; i += p)` `                ``isPrime[i] = ``false``;` `        ``}` `    ``}` `}`   `// Function to check if we can` `// represent N as product of a prime` `// and a composite number or not` `bool` `isRepresentable(``int` `N)` `{`   `    ``// Generating primes using Sieve` `    ``bool` `isPrime[N + 1];`   `    ``SieveOfEratosthenes(N, isPrime);`   `    ``// Traversing through the array` `    ``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``;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 52;` `    ``if` `(isRepresentable(N)) {` `        ``cout << ``"Yes"``;` `    ``}` `    ``else` `{` `        ``cout << ``"No"``;` `    ``}` `    ``return` `0;` `}`

## Java

 `// Java program to implement the above approach` `import` `java.util.*;` `public` `class` `GFG` `{` `  `  `// Function to generate all prime` `// numbers less than N` `static` `void` `SieveOfEratosthenes(``int` `N, ``boolean` `[]isPrime)` `{` `  `  `    ``// Initialize all entries of boolean array` `    ``// as true. A value in isPrime[i] will finally` `    ``// be false if i is Not a prime, else true` `    ``// bool isPrime[N+1];` `    ``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] is not changed,` `        ``// then it is a prime` `        ``if` `(isPrime[p] == ``true``) {`   `            ``// Update all multiples of p` `            ``for` `(``int` `i = p * ``2``; i <= N; i += p)` `                ``isPrime[i] = ``false``;` `        ``}` `    ``}` `}`   `// Function to check if we can` `// represent N as product of a prime` `// and a composite number or not` `static` `boolean` `isRepresentable(``int` `N)` `{`   `    ``// Generating primes using Sieve` `    ``boolean` `[]isPrime = ``new` `boolean``[N + ``1``];`   `    ``SieveOfEratosthenes(N, isPrime);`   `    ``// Traversing through the array` `    ``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``;` `}`   `// Driver Code` `public` `static` `void` `main(String arg[])` `{` `    ``int` `N = ``52``;` `    ``if` `(isRepresentable(N)) {` `        ``System.out.println(``"Yes"``);` `    ``}` `    ``else` `{` `        ``System.out.println(``"No"``);` `    ``}` `}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Python3

 `# python program for the above approach` `import` `math`   `# Function to generate all prime` `# numbers less than N` `def` `SieveOfEratosthenes(N, isPrime):`   `    ``# Initialize all entries of boolean array` `    ``# as true. A value in isPrime[i] will finally` `    ``# be false if i is Not a prime, else true` `    ``# bool isPrime[N+1];` `    ``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] is not changed,` `        ``# then it is a prime` `        ``if` `(isPrime[p] ``=``=` `True``):`   `            ``# Update all multiples of p` `            ``for` `i ``in` `range``(p``+``2``, N``+``1``, p):` `                ``isPrime[i] ``=` `False`   `# Function to check if we can` `# represent N as product of a prime` `# and a composite number or not` `def` `isRepresentable(N):`   `    ``# Generating primes using Sieve` `    ``isPrime ``=` `[``0` `for` `_ ``in` `range``(N ``+` `1``)]`   `    ``SieveOfEratosthenes(N, isPrime)`   `    ``# Traversing through the array` `    ``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`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``N ``=` `52` `    ``if` `(isRepresentable(N)):` `        ``print``(``"Yes"``)`   `    ``else``:` `        ``print``(``"No"``)`   `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# program to implement the above approach` `using` `System;` `class` `GFG` `{` `// Function to generate all prime` `// numbers less than N` `static` `void` `SieveOfEratosthenes(``int` `N, ``bool` `[]isPrime)` `{` `    ``// Initialize all entries of boolean array` `    ``// as true. A value in isPrime[i] will finally` `    ``// be false if i is Not a prime, else true` `    ``// bool isPrime[N+1];` `    ``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] is not changed,` `        ``// then it is a prime` `        ``if` `(isPrime[p] == ``true``) {`   `            ``// Update all multiples of p` `            ``for` `(``int` `i = p * 2; i <= N; i += p)` `                ``isPrime[i] = ``false``;` `        ``}` `    ``}` `}`   `// Function to check if we can` `// represent N as product of a prime` `// and a composite number or not` `static` `bool` `isRepresentable(``int` `N)` `{`   `    ``// Generating primes using Sieve` `    ``bool` `[]isPrime = ``new` `bool``[N + 1];`   `    ``SieveOfEratosthenes(N, isPrime);`   `    ``// Traversing through the array` `    ``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``;` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int` `N = 52;` `    ``if` `(isRepresentable(N)) {` `        ``Console.Write(``"Yes"``);` `    ``}` `    ``else` `{` `        ``Console.Write(``"No"``);` `    ``}` `}` `}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`Yes`

Time complexity: O(N*log(logN))
Auxiliary Space: O(N)

Another Approach

Below is the implementation of the above approach:

## C++

 `#include ` `#include `   `// Function to check if a number is prime` `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``;` `}`   `// Function to check if N can be represented as the product of a prime and a composite number` `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;`   `            ``// Check if both factors are prime and composite numbers` `            ``if` `(isPrime(factor1) && !isPrime(factor2))` `                ``return` `true``;` `            ``if` `(!isPrime(factor1) && isPrime(factor2))` `                ``return` `true``;` `        ``}` `    ``}`   `    ``return` `false``;` `}` `// Driver Code` `int` `main() {` `    ``int` `N=52;` `    ``if` `(isProductOfPrimeAndComposite(N))` `        ``std::cout << ``"Yes"` `<< std::endl;` `    ``else` `        ``std::cout << ``"No"` `<< std::endl;`   `    ``return` `0;` `}`

Output

```Yes
```

Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up