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:
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.
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 seive 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.
- Check for these two conditions:
- Print the answer, according to the above observation.
Below is the implementation of the above approach.
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to generate all prime// numbers less than Nvoid 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 notbool 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 Codeint main(){ int N = 52; if (isRepresentable(N)) { cout << "Yes"; } else { cout << "No"; } return 0;} |
Java
// Java program to implement the above approachimport java.util.*;public class GFG{ // Function to generate all prime// numbers less than Nstatic 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 notstatic 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 Codepublic 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 approachimport math# Function to generate all prime# numbers less than Ndef 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 notdef 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 Codeif __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 approachusing System;class GFG{// Function to generate all prime// numbers less than Nstatic 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 notstatic 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 Codepublic 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
<script>// Javascript program for the above approach// Function to generate all prime// numbers less than Nfunction 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] = isPrime[1] = false; for (let i = 2; i <= N; i++) isPrime[i] = true; for (let 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 (let 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 notfunction isRepresentable(N){ // Generating primes using Sieve let isPrime = []; SieveOfEratosthenes(N, isPrime); // Traversing through the array 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;}// Driver Codelet N = 52;if (isRepresentable(N)) { document.write("Yes");}else { document.write("No");}// This code is contributed by Samim Hossain Mondal.</script> |
Yes
Time complexity: O(N*log(logN))
Auxiliary Space: O(N)

