📜  检查是否存在正好有N个因子和K个素因子的数

📅  最后修改于: 2021-04-22 00:38:26             🧑  作者: Mango

给定两个数字NK ,任务是查找整数X是否存在,使得它恰好具有N个因子,并且其中的K是质数。
例子:

方法:想法是使用以下身份。

  • 对于任何数字X,如果该数字具有N个因数,其中K是素数:
X = k1a + k2b + k3c + ... + knn
  • 因子总数N等于:
N = (a + 1) * (b + 1) * (c + 1) .. (n + 1)
  • 因此,该想法是检查N是否可以表示为K个大于1的整数的乘积。这可以通过找到数字N的除数来完成。
  • 如果此计数小于K,则不可能给出答案。否则,这是可能的。

下面是上述方法的实现:

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 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 v = new List();
 
    // 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


输出:
Yes