📜  一个数恰好具有x个因子的质数的最大数量

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

给定整数X ,表示正整数N可以具有的因子数。任务是找到数量N可以具有的最大素数的最大数量。

例子:

方法:问题中的关键发现是,任何正自然数都可以表示为其主要因子的乘积,如下所示:

//数字可以表示为乘积//素因数如下N = a^p * b^q * c^r .. // N的因子总数可以//定义如下:因子数=(p + 1)*(q + 1)*(r + 1)。

在上述问题中,给出了因子数量,这些因子可用于查找具有给定因子数量的数字可能的最大素数,如下所示:

If X can be expressed as product of K numbers then we have at most K primes in X.
In Order to split X as product of maximum number of values,
all the values should be prime.

X = (p+1) * (q+1) * (r+1)

// So the maximum number of prime
// factors of the given number greater
// than 1 can lead to a number N.
Let's say X = 12
X = 2 * 2 * 3
Then possible N can be:
N = a(2-1) * b(2-1) * c(3-1)
N = a1 * b1 * c2

// Here a, b, and c can be any distinct prime
// numbers to get the possible value of N
N = 21 * 31 * 52
N = 150

let's say X = 8
X = 2 * 2 * 2
N = 21 * 31 * 51
N  = 30

因此,一个数的质数除数的最大数量是该数的因数的因式分解中的质因数的计数(也可以是重复的)。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum count of the prime factors
// by the count of factors of number
 
#include 
#include 
 
using namespace std;
 
// Function to count number
// of prime factors of x
int countPrimeFactors(int n)
{
    if (n == 1)
        return 0;
 
    // Count variable is
    // incremented for every
    // prime factor of x
    int cnt = 0;
    while (n % 2 == 0) {
        cnt++;
        n = n / 2;
    }
 
    // Loop to count the number
    // of the prime factors of
    // the given number
    for (int i = 3; i <= sqrt(n);
         i += 2) {
        while (n % i == 0) {
            cnt++;
            n = n / i;
        }
    }
 
    if (n > 2)
        cnt++;
 
    return cnt;
}
 
// Driver Code
int main()
{
    int x = 8;
    int prime_factor_cnt = countPrimeFactors(x);
    cout << prime_factor_cnt << endl;
    return 0;
}


Java
// Java implementation to find the
// maximum count of the prime factors
// by the count of factors of number
class GFG {
 
    // Function to count number
    // of prime factors of x
    static int countPrimeFactors(int n)
    {
        if (n == 1)
            return 0;
 
        // Count variable is
        // incremented form every
        // prime factor of x
        int cnt = 0;
        while (n % 2 == 0) {
            cnt++;
            n = n / 2;
        }
 
        // Loop to count the number
        // of the prime factors of
        // the given number
        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            while (n % i == 0) {
                cnt++;
                n = n / i;
            }
        }
 
        if (n > 2)
            cnt++;
        return cnt;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int x = 8;
        int prime_factor_cnt = countPrimeFactors(x);
        System.out.print(prime_factor_cnt + "\n");
    }
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation to find the
# maximum count of the prime factors
# by the count of factors of number
import math
 
# Function to count number
# of prime factors of x
def countPrimeFactors(n):
     
    if (n == 1):
        return 0
         
    # Count variable is
    # incremented form every
    # prime factor of x
    cnt = 0
     
    while (n % 2 == 0):
        cnt += 1
        n = n // 2
         
    # Loop to count the number
    # of the prime factors of
    # the given number
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        while (n % i == 0):
            cnt += 1
            n = n // i
     
    if (n > 2):
        cnt += 1
     
    return cnt
 
# Driver Code
x = 8
prime_factor_cnt = countPrimeFactors(x)
 
print(prime_factor_cnt)
 
# This code is contributed by ShubhamCoder


C#
// C# implementation to find the
// maximum count of the prime factors
// by the count of factors of number
using System;
 
class GFG {
 
    // Function to count number
    // of prime factors of x
    static int countPrimeFactors(int n)
    {
        if (n == 1)
            return 0;
 
        // Count variable is
        // incremented form every
        // prime factor of x
        int cnt = 0;
        while (n % 2 == 0) {
            cnt++;
            n = n / 2;
        }
 
        // Loop to count the number
        // of the prime factors of
        // the given number
        for (int i = 3;
             i <= Math.Sqrt(n); i += 2) {
            while (n % i == 0) {
                cnt++;
                n = n / i;
            }
        }
 
        if (n > 2)
            cnt++;
 
        return cnt;
    }
 
    // Driver Code
    static public void Main()
    {
        int x = 8;
        int prime_factor_cnt = countPrimeFactors(x);
        Console.Write(prime_factor_cnt);
    }
}
 
// This code is contributed by ShubhamCoder


Javascript


输出:
3

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。