Related Articles
Check if a number exists having exactly N factors and K prime factors
• Difficulty Level : Expert
• Last Updated : 12 Apr, 2021

Given two numbers N and K, the task is to find whether an integer X exists such that it has exactly N factors and K out of them are prime.
Examples:

Input: N = 4, K = 2
Output: Yes
Explanation:
One possible number for X is 6.
The number 6 has a total 4 factors: 1, 2, 3 & 6.
It also has exactly 2 prime factors: 2 & 3.
Input: N = 3, K = 1
Output: Yes
Explanation:
One possible number for X is 49.
The number 49 has a total 3 factors: 1, 7, & 49.
It also has exactly 1 prime factor: 7.

Approach: The idea is to use the following identity.

• For any number X, if the number has N factors out of which K are prime:
`X = k1a + k2b + k3c + ... + knn`
• The total number of factors N is equal to:
`N = (a + 1) * (b + 1) * (c + 1) .. (n + 1)`
• Therefore, the idea is to check if N can be represented as a product of K integers greater than 1. This can be done by finding the divisors of the number N.
• If the count of this is less than K, then the answer is not possible. Else, it is possible.

Below is the implementation of the above approach:

## C++

 `// C++ program to check if it``// is possible to make a number``// having total N factors and``// K prime factors` `#include ``using` `namespace` `std;` `// Function to compute the``// number of factors of``// the given number``bool` `factors(``int` `n, ``int` `k)``{``    ``// Vector to store``    ``// the prime factors``    ``vector<``int``> v;` `    ``// While there are no``    ``// two multiples in``    ``// the number, divide``    ``// it by 2``    ``while` `(n % 2 == 0) {``        ``v.push_back(2);``        ``n /= 2;``    ``}` `    ``// If the size is already``    ``// greater than K,``    ``// then return true``    ``if` `(v.size() >= k)``        ``return` `true``;` `    ``// Computing the remaining``    ``// divisors of the number``    ``for` `(``int` `i = 3; i * i <= n;``         ``i += 2) {` `        ``// If n is divisible by i,``        ``// then it is a divisor``        ``while` `(n % i == 0) {``            ``n = n / i;``            ``v.push_back(i);``        ``}` `        ``// If the size is already``        ``// greater than K, then``        ``// return true``        ``if` `(v.size() >= k)``            ``return` `true``;``    ``}` `    ``if` `(n > 2)``        ``v.push_back(n);` `    ``// If the size is already``    ``// greater than K,``    ``// then return true``    ``if` `(v.size() >= k)``        ``return` `true``;` `    ``// If none of the above``    ``// conditions satisfies,``    ``// then return false``    ``return` `false``;``}` `// Function to check if it is``// possible to make a number``// having total N factors and``// K prime factors``void` `operation(``int` `n, ``int` `k)``{``    ``bool` `answered = ``false``;` `    ``// If total divisors are``    ``// less than the number``    ``// of prime divisors,``    ``// then print No``    ``if` `(n < k) {``        ``answered = ``true``;``        ``cout << ``"No"``             ``<< ``"\n"``;``    ``}` `    ``// Find the number of``    ``// factors of n``    ``bool` `ok = factors(n, k);` `    ``if` `(!ok && !answered) {``        ``answered = ``true``;``        ``cout << ``"No"``             ``<< ``"\n"``;``    ``}` `    ``if` `(ok && !answered)``        ``cout << ``"Yes"``             ``<< ``"\n"``;``}` `// Driver Code``int` `main()``{``    ``int` `n = 4;``    ``int` `k = 2;` `    ``operation(n, k);``    ``return` `0;``}`

## Java

 `// Java program to check if it``// is possible to make a number``// having total N factors and``// K prime factors``import` `java.io.*;``import` `java.util.*;` `class` `GFG{``    ` `// Function to compute the``// number of factors of``// the given number``static` `boolean` `factors(``int` `n, ``int` `k)``{``    ` `    ``// Vector to store``    ``// the prime factors``    ``ArrayList v = ``new` `ArrayList();` `    ``// While there are no``    ``// two multiples in``    ``// the number, divide``    ``// it by 2``    ``while` `(n % ``2` `== ``0``)``    ``{``        ``v.add(``2``);``        ``n /= ``2``;``    ``}` `    ``// If the size is already``    ``// greater than K,``    ``// then return true``    ``if` `(v.size() >= k)``        ``return` `true``;` `    ``// Computing the remaining``    ``// divisors of the number``    ``for``(``int` `i = ``3``; i * i <= n; i += ``2``)``    ``{``       ` `       ``// If n is divisible by i,``       ``// then it is a divisor``       ``while` `(n % i == ``0``)``       ``{``           ``n = n / i;``           ``v.add(i);``       ``}``       ` `       ``// If the size is already``       ``// greater than K, then``       ``// return true``       ``if` `(v.size() >= k)``           ``return` `true``;``    ``}` `    ``if` `(n > ``2``)``        ``v.add(n);` `    ``// If the size is already``    ``// greater than K,``    ``// then return true``    ``if` `(v.size() >= k)``        ``return` `true``;` `    ``// If none of the above``    ``// conditions satisfies,``    ``// then return false``    ``return` `false``;``}``    ` `// Function to check if it is``// possible to make a number``// having total N factors and``// K prime factors``static` `void` `operation(``int` `n, ``int` `k)``{``    ``boolean` `answered = ``false``;` `    ``// If total divisors are``    ``// less than the number``    ``// of prime divisors,``    ``// then print No``    ``if` `(n < k)``    ``{``        ``answered = ``true``;``        ``System.out.println(``"No"``);``    ``}` `    ``// Find the number of``    ``// factors of n``    ``boolean` `ok = factors(n, k);` `    ``if` `(!ok && !answered)``    ``{``        ``answered = ``true``;``        ``System.out.println(``"No"``);``    ``}``    ``if` `(ok && !answered)``        ``System.out.println(``"Yes"``);``}``    ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``4``;``    ``int` `k = ``2``;``    ` `    ``//Function call``    ``operation(n, k);``}``}` `// This code is contributed by coder001`

## Python3

 `# Python3 program to check if it``# is possible to make a number``# having total N factors and``# K prime factors` `# Function to compute the``# number of factors of``# the given number``def` `factors(n, k):` `    ``# Vector to store``    ``# the prime factors``    ``v ``=` `[];` `    ``# While there are no``    ``# two multiples in``    ``# the number, divide``    ``# it by 2``    ``while` `(n ``%` `2` `=``=` `0``):``        ``v.append(``2``);``        ``n ``/``/``=` `2``;``    ` `    ``# If the size is already``    ``# greater than K,``    ``# then return true``    ``if` `(``len``(v) >``=` `k):``        ``return` `True``;` `    ``# Computing the remaining``    ``# divisors of the number``    ``for` `i ``in` `range``(``3``, ``int``(n ``*``*` `(``1` `/` `2``)), ``2``):` `        ``# If n is divisible by i,``        ``# then it is a divisor``        ``while` `(n ``%` `i ``=``=` `0``):``            ``n ``=` `n ``/``/` `i;``            ``v.append(i);` `        ``# If the size is already``        ``# greater than K, then``        ``# return true``        ``if` `(``len``(v) >``=` `k):``            ``return` `True``;``    ` `    ``if` `(n > ``2``):``        ``v.append(n);` `    ``# If the size is already``    ``# greater than K,``    ``# then return true``    ``if` `(``len``(v) >``=` `k):``        ``return` `True``;` `    ``# If none of the above``    ``# conditions satisfies,``    ``# then return false``    ``return` `False``;` `# Function to check if it is``# possible to make a number``# having total N factors and``# K prime factors``def` `operation(n, k):``    ``answered ``=` `False``;` `    ``# If total divisors are``    ``# less than the number``    ``# of prime divisors,``    ``# then print No``    ``if` `(n < k):``        ``answered ``=` `True``;``        ``print``(``"No"``);` `    ``# Find the number of``    ``# factors of n``    ``ok ``=` `factors(n, k);` `    ``if` `(``not` `ok ``and` `not` `answered):``        ``answered ``=` `True``;``        ``print``(``"No"``);` `    ``if` `(ok ``and` `not` `answered):``        ``print``(``"Yes"``);` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``n ``=` `4``;``    ``k ``=` `2``;``    ``operation(n, k);` `# This code is contributed by AnkitRai01`

## C#

 `// C# program to check if it``// is possible to make a number``// having total N factors and``// K prime factors``using` `System;``using` `System.Collections.Generic;` `class` `GFG{``    ` `// Function to compute the``// number of factors of``// the given number``static` `bool` `factors(``int` `n, ``int` `k)``{``    ` `    ``// Vector to store``    ``// the prime factors``    ``List<``int``> v = ``new` `List<``int``>();` `    ``// While there are no``    ``// two multiples in``    ``// the number, divide``    ``// it by 2``    ``while` `(n % 2 == 0)``    ``{``        ``v.Add(2);``        ``n /= 2;``    ``}` `    ``// If the size is already``    ``// greater than K,``    ``// then return true``    ``if` `(v.Count >= k)``        ``return` `true``;` `    ``// Computing the remaining``    ``// divisors of the number``    ``for``(``int` `i = 3; i * i <= n; i += 2)``    ``{``        ` `        ``// If n is divisible by i,``        ``// then it is a divisor``        ``while` `(n % i == 0)``        ``{``            ``n = n / i;``            ``v.Add(i);``        ``}``            ` `        ``// If the size is already``        ``// greater than K, then``        ``// return true``        ``if` `(v.Count >= k)``            ``return` `true``;``    ``}` `    ``if` `(n > 2)``        ``v.Add(n);` `    ``// If the size is already``    ``// greater than K,``    ``// then return true``    ``if` `(v.Count >= k)``        ``return` `true``;` `    ``// If none of the above``    ``// conditions satisfies,``    ``// then return false``    ``return` `false``;``}``    ` `// Function to check if it is``// possible to make a number``// having total N factors and``// K prime factors``static` `void` `operation(``int` `n, ``int` `k)``{``    ``bool` `answered = ``false``;` `    ``// If total divisors are``    ``// less than the number``    ``// of prime divisors,``    ``// then print No``    ``if` `(n < k)``    ``{``        ``answered = ``true``;``        ``Console.WriteLine(``"No"``);``    ``}` `    ``// Find the number of``    ``// factors of n``    ``bool` `ok = factors(n, k);` `    ``if` `(!ok && !answered)``    ``{``        ``answered = ``true``;``        ``Console.WriteLine(``"No"``);``    ``}``    ``if` `(ok && !answered)``        ``Console.WriteLine(``"Yes"``);``}``    ` `// Driver code``public` `static` `void` `Main()``{``    ``int` `n = 4;``    ``int` `k = 2;``    ` `    ``// Function call``    ``operation(n, k);``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 ``
Output:
`Yes`

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.

My Personal Notes arrow_drop_up