Check if a number exists having exactly N factors and K prime factors

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.



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if it
// is possible to make a number
// having total N factors and
// K prime factors
  
#include <bits/stdc++.h>
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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer> v = new ArrayList<Integer>();
  
    // 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

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

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.





Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, coder001

Article Tags :
Practice Tags :