Given two integers **X** and **K**, the task is to determine whether there exists a number that has exactly **X** factors out of which **K** is prime.

**Examples:**

Input:X = 8, K = 1Output:YesExplanation:

The number is 128

Factors of 128 = {1, 2, 4, 8, 16, 32, 64, 128} which are 8 in count = X

Among these, only 2 is prime. Therefore count of prime factor = 1 = K

Input:X = 4, K = 2Output:YesExplanation:

The number is 6

Factors of 6 = {1, 2, 3, 6} which are 4 in count = X

Among these, only 2 and 3 are prime. Therefore count of prime factor = 2 = K

**Approach:**

- Suppose a number
**N**has**X**factors out of which**K**are prime, say - Thus, number can be written as
where, the total number of factors is calculated by - It is observed that X is a product of “
**power+1**” of the prime factors of the number. Thus, if we are able to divide**X**into a product of**K**numbers, then we can form a number with exactly**X**factors out of which**K**is prime.

Below is the implementation of the above approach:

`// C++ program to check if there exists ` `// a number with X factors ` `// out of which exactly K are prime ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to check if such number exists ` `bool` `check(` `int` `X, ` `int` `K) `
`{ ` ` ` `int` `prime, temp, sqr, i; `
` ` ` ` `// To store the sum of powers `
` ` `// of prime factors of X which `
` ` `// determines the maximum count `
` ` `// of numbers whose product can form X `
` ` `prime = 0; `
` ` `temp = X; `
` ` `sqr = ` `sqrt` `(X); `
` ` ` ` `// Determining the prime factors of X `
` ` `for` `(i = 2; i <= sqr; i++) { `
` ` ` ` `while` `(temp % i == 0) { `
` ` `temp = temp / i; `
` ` `prime++; `
` ` `} `
` ` `} `
` ` ` ` `// To check if the number is prime `
` ` `if` `(temp > 2) `
` ` `prime++; `
` ` ` ` `// If X is 1, then we cannot form `
` ` `// a number with 1 factor and K `
` ` `// prime factor (as K is atleast 1) `
` ` `if` `(X == 1) `
` ` `return` `false` `; `
` ` ` ` `// If X itself is prime then it `
` ` `// can be represented as a power `
` ` `// of only 1 prime factor which `
` ` `// is X itself so we return true `
` ` `if` `(prime == 1 && K == 1) `
` ` `return` `true` `; `
` ` ` ` `// If sum of the powers of prime factors `
` ` `// of X is greater than or equal to K, `
` ` `// which means X can be represented as a `
` ` `// product of K numbers, we return true `
` ` `else` `if` `(prime >= K) `
` ` `return` `true` `; `
` ` ` ` `// In any other case, we return false `
` ` `// as we cannot form a number with X `
` ` `// factors and K prime factors `
` ` `else`
` ` `return` `false` `; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `X, K; `
` ` `X = 4; `
` ` `K = 2; `
` ` ` ` `if` `(check(X, K)) `
` ` `cout << ` `"Yes"` `; `
` ` `else`
` ` `cout << ` `"No"` `; `
`} ` |

*chevron_right*

*filter_none*

`// Java program to check if there exists ` `// a number with X factors ` `// out of which exactly K are prime ` ` ` ` ` `import` `java.util.*; `
` ` `class` `GFG{ `
` ` `// Function to check if such number exists ` `static` `boolean` `check(` `int` `X, ` `int` `K) `
`{ ` ` ` `int` `prime, temp, sqr, i; `
` ` ` ` `// To store the sum of powers `
` ` `// of prime factors of X which `
` ` `// determines the maximum count `
` ` `// of numbers whose product can form X `
` ` `prime = ` `0` `; `
` ` `temp = X; `
` ` `sqr = (` `int` `) Math.sqrt(X); `
` ` ` ` `// Determining the prime factors of X `
` ` `for` `(i = ` `2` `; i <= sqr; i++) { `
` ` ` ` `while` `(temp % i == ` `0` `) { `
` ` `temp = temp / i; `
` ` `prime++; `
` ` `} `
` ` `} `
` ` ` ` `// To check if the number is prime `
` ` `if` `(temp > ` `2` `) `
` ` `prime++; `
` ` ` ` `// If X is 1, then we cannot form `
` ` `// a number with 1 factor and K `
` ` `// prime factor (as K is atleast 1) `
` ` `if` `(X == ` `1` `) `
` ` `return` `false` `; `
` ` ` ` `// If X itself is prime then it `
` ` `// can be represented as a power `
` ` `// of only 1 prime factor which `
` ` `// is X itself so we return true `
` ` `if` `(prime == ` `1` `&& K == ` `1` `) `
` ` `return` `true` `; `
` ` ` ` `// If sum of the powers of prime factors `
` ` `// of X is greater than or equal to K, `
` ` `// which means X can be represented as a `
` ` `// product of K numbers, we return true `
` ` `else` `if` `(prime >= K) `
` ` `return` `true` `; `
` ` ` ` `// In any other case, we return false `
` ` `// as we cannot form a number with X `
` ` `// factors and K prime factors `
` ` `else`
` ` `return` `false` `; `
`} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) `
`{ ` ` ` `int` `X, K; `
` ` `X = ` `4` `; `
` ` `K = ` `2` `; `
` ` ` ` `if` `(check(X, K)) `
` ` `System.out.print(` `"Yes"` `); `
` ` `else`
` ` `System.out.print(` `"No"` `); `
`} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

`# Python3 program to check if there exists ` `# a number with X factors ` `# out of which exactly K are prime ` ` ` `from` `math ` `import` `sqrt `
`# Function to check if such number exists ` `def` `check(X,K): `
` ` ` ` `# To store the sum of powers `
` ` `# of prime factors of X which `
` ` `# determines the maximum count `
` ` `# of numbers whose product can form X `
` ` `prime ` `=` `0`
` ` `temp ` `=` `X `
` ` `sqr ` `=` `int` `(sqrt(X)) `
` ` ` ` `# Determining the prime factors of X `
` ` `for` `i ` `in` `range` `(` `2` `,sqr` `+` `1` `,` `1` `): `
` ` `while` `(temp ` `%` `i ` `=` `=` `0` `): `
` ` `temp ` `=` `temp ` `/` `/` `i `
` ` `prime ` `+` `=` `1`
` ` ` ` `# To check if the number is prime `
` ` `if` `(temp > ` `2` `): `
` ` `prime ` `+` `=` `1`
` ` ` ` `# If X is 1, then we cannot form `
` ` `# a number with 1 factor and K `
` ` `# prime factor (as K is atleast 1) `
` ` `if` `(X ` `=` `=` `1` `): `
` ` `return` `False`
` ` ` ` `# If X itself is prime then it `
` ` `# can be represented as a power `
` ` `# of only 1 prime factor w0hich `
` ` `# is X itself so we return true `
` ` `if` `(prime ` `=` `=` `1` `and` `K ` `=` `=` `1` `): `
` ` `return` `True`
` ` ` ` `# If sum of the powers of prime factors `
` ` `# of X is greater than or equal to K, `
` ` `# which means X can be represented as a `
` ` `# product of K numbers, we return true `
` ` `elif` `(prime >` `=` `K): `
` ` `return` `True`
` ` ` ` `# In any other case, we return false `
` ` `# as we cannot form a number with X `
` ` `# factors and K prime factors `
` ` `else` `: `
` ` `return` `False`
` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` `X ` `=` `4`
` ` `K ` `=` `2`
` ` ` ` `if` `(check(X, K)): `
` ` `print` `(` `"Yes"` `) `
` ` `else` `: `
` ` `print` `(` `"No"` `) `
` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

`// C# program to check if there exists ` `// a number with X factors ` `// out of which exactly K are prime ` `using` `System; `
` ` `class` `GFG{ `
` ` ` ` `// Function to check if such number exists `
` ` `static` `bool` `check(` `int` `X, ` `int` `K) `
` ` `{ `
` ` `int` `prime, temp, sqr, i; `
` ` ` ` `// To store the sum of powers `
` ` `// of prime factors of X which `
` ` `// determines the maximum count `
` ` `// of numbers whose product can form X `
` ` `prime = 0; `
` ` `temp = X; `
` ` `sqr = Convert.ToInt32(Math.Sqrt(X)); `
` ` ` ` `// Determining the prime factors of X `
` ` `for` `(i = 2; i <= sqr; i++) { `
` ` ` ` `while` `(temp % i == 0) { `
` ` `temp = temp / i; `
` ` `prime++; `
` ` `} `
` ` `} `
` ` ` ` `// To check if the number is prime `
` ` `if` `(temp > 2) `
` ` `prime++; `
` ` ` ` `// If X is 1, then we cannot form `
` ` `// a number with 1 factor and K `
` ` `// prime factor (as K is atleast 1) `
` ` `if` `(X == 1) `
` ` `return` `false` `; `
` ` ` ` `// If X itself is prime then it `
` ` `// can be represented as a power `
` ` `// of only 1 prime factor which `
` ` `// is X itself so we return true `
` ` `if` `(prime == 1 && K == 1) `
` ` `return` `true` `; `
` ` ` ` `// If sum of the powers of prime factors `
` ` `// of X is greater than or equal to K, `
` ` `// which means X can be represented as a `
` ` `// product of K numbers, we return true `
` ` `else` `if` `(prime >= K) `
` ` `return` `true` `; `
` ` ` ` `// In any other case, we return false `
` ` `// as we cannot form a number with X `
` ` `// factors and K prime factors `
` ` `else`
` ` `return` `false` `; `
` ` `} `
` ` ` ` `// Driver code `
` ` `static` `public` `void` `Main () `
` ` `{ `
` ` `int` `X, K; `
` ` `X = 4; `
` ` `K = 2; `
` ` ` ` `if` `(check(X, K)) `
` ` `Console.WriteLine(` `"Yes"` `); `
` ` `else`
` ` `Console.WriteLine(` `"No"` `); `
` ` `} `
`} ` ` ` `// This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Check if there exists a prime number which gives Y after being repeatedly subtracted from X
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Check if a number exists with X divisors out of which Y are composite
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Number which has the maximum number of distinct prime factors in the range M to N
- Check whether a number has exactly three distinct factors or not
- Find number of factors of N when location of its two factors whose product is N is given
- Number of factors of very large number N modulo M where M is any prime number
- Queries to find whether a number has exactly four distinct factors or not
- Check if all Prime factors of number N are unique or not
- Numbers less than N which are product of exactly two distinct prime numbers
- Count of subsequences which consists exactly K prime numbers
- Find if there exists multiple ways to draw line through (x, y) to cut rectangle in equal halfs
- Check if a number can be expressed as a product of exactly K prime divisors
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Find out the prime numbers in the form of A+nB or B+nA
- Check if each element of the given array is the product of exactly K prime numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

**Practice Tags :**