📌  相关文章
📜  检查是否存在一个X因子除以Y的复合数

📅  最后修改于: 2021-06-25 20:13:51             🧑  作者: Mango

给定两个整数XY分别表示除数的总数和复合除数的数量,任务是检查是否存在整数N ,该整数正好具有X个除数, Y是复合数。

例子:

方法:

  1. 首先计算一个数的质数除数,它等于:

主除数的数量=除数的总数–复合除数的数量– 1

  1. 因此,素数除数C = XY – 1
  2. 由于每个数字都有1作为因数,而1既不是质数也不是复合数,我们必须将其排除在素数的数量之外。
  3. 如果复合除数的数量小于主除数的数量,则根本找不到这样的数量。
  4. 因此,如果X的素数分解至少包含C个不同的整数,则可能有解决方案。否则,我们将找不到满足给定条件的数字N。
  5. 找到最大数量的值X可以分解为每个值都大于1 。换句话说,我们可以找出X的素因式分解。
  6. 如果该素数分解的项数大于或等于C ,那么这样的数是可能的。

下面是上述方法的实现:

C++
// C++ program to check if a number
// exists having exactly X positive
// divisors out of which Y are
// composite divisors
#include 
using namespace std;
 
int factorize(int N)
{
    int count = 0;
    int cnt = 0;
 
    // Count the number of
    // times 2 divides N
    while ((N % 2) == 0) {
        N = N / 2;
        count++;
    }
 
    cnt = cnt + count;
 
    // check for all possible
    // numbers that can divide it
    for (int i = 3; i <= sqrt(N); i += 2) {
        count = 0;
        while (N % i == 0) {
            count++;
            N = N / i;
        }
        cnt = cnt + count;
    }
 
    // if N at the end
    // is a prime number.
    if (N > 2)
        cnt = cnt + 1;
    return cnt;
}
 
// Function to check if any
// such number exists
void ifNumberExists(int X, int Y)
{
    int C, dsum;
 
    C = X - Y - 1;
    dsum = factorize(X);
    if (dsum >= C)
        cout << "YES \n";
    else
        cout << "NO \n";
}
 
// Driver Code
int main()
{
 
    int X, Y;
    X = 6;
    Y = 4;
 
    ifNumberExists(X, Y);
    return 0;
}


Java
// Java program to check if a number
// exists having exactly X positive
// divisors out of which Y are
// composite divisors
import java.lang.Math;
class GFG{
     
public static int factorize(int N)
{
    int count = 0;
    int cnt = 0;
     
    // Count the number of
    // times 2 divides N
    while ((N % 2) == 0)
    {
        N = N / 2;
        count++;
    }
     
    cnt = cnt + count;
     
    // Check for all possible
    // numbers that can divide it
    for(int i = 3; i <= Math.sqrt(N); i += 2)
    {
       count = 0;
       while (N % i == 0)
       {
           count++;
           N = N / i;
       }
       cnt = cnt + count;
    }
     
    // If N at the end
    // is a prime number.
    if (N > 2)
        cnt = cnt + 1;
    return cnt;
}
     
// Function to check if any
// such number exists
public static void ifNumberExists(int X, int Y)
{
    int C, dsum;
    C = X - Y - 1;
    dsum = factorize(X);
         
    if (dsum >= C)
        System.out.println("YES");
    else
        System.out.println("NO");
}
 
// Driver code   
public static void main(String[] args)
{
    int X, Y;
    X = 6;
    Y = 4;
     
    ifNumberExists(X, Y);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to check if a number exists
# having exactly X positive divisors out of
#  which Y are composite divisors
import math
 
def factorize(N):
 
    count = 0
    cnt = 0
 
    # Count the number of
    # times 2 divides N
    while ((N % 2) == 0):
        N = N // 2
        count+=1
 
    cnt = cnt + count
 
    # Check for all possible
    # numbers that can divide it
    sq = int(math.sqrt(N))
    for i in range(3, sq, 2):
        count = 0
         
        while (N % i == 0):
            count += 1
            N = N // i
         
        cnt = cnt + count
 
    # If N at the end
    # is a prime number.
    if (N > 2):
        cnt = cnt + 1
    return cnt
 
# Function to check if any
# such number exists
def ifNumberExists(X, Y):
 
    C = X - Y - 1
    dsum = factorize(X)
     
    if (dsum >= C):
        print ("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == "__main__":
     
    X = 6
    Y = 4
 
    ifNumberExists(X, Y)
 
# This code is contributed by chitranayal


C#
// C# program to check if a number
// exists having exactly X positive
// divisors out of which Y are
// composite divisors
using System;
class GFG{
 
public static int factorize(int N)
{
    int count = 0;
    int cnt = 0;
     
    // Count the number of
    // times 2 divides N
    while ((N % 2) == 0)
    {
        N = N / 2;
        count++;
    }
     
    cnt = cnt + count;
     
    // Check for all possible
    // numbers that can divide it
    for(int i = 3; i <= Math.Sqrt(N); i += 2)
    {
        count = 0;
        while (N % i == 0)
        {
            count++;
            N = N / i;
        }
        cnt = cnt + count;
    }
     
    // If N at the end
    // is a prime number.
    if (N > 2)
        cnt = cnt + 1;
    return cnt;
}
     
// Function to check if any
// such number exists
public static void ifNumberExists(int X, int Y)
{
    int C, dsum;
    C = X - Y - 1;
    dsum = factorize(X);
         
    if (dsum >= C)
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
 
// Driver code
public static void Main(string[] args)
{
    int X, Y;
    X = 6;
    Y = 4;
     
    ifNumberExists(X, Y);
}
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
YES

时间复杂度: O(N 1/2 )
辅助空间: O(1)