Maximum number of prime factors a number can have with exactly x factors

Given an integer X, denoting the number of factors of a positive integer N can have. The task is to find the maximum number of distinct prime factors the number N can have.Â
Examples:Â
Input: X = 9Â
Output: 2Â
Explanation:Â
Some of the possible numbers having 9 factors are:Â
256: 1, 2, 4, 8, 16, 32, 64, 128, 256Â
Number of prime factors = 1Â
36: 1, 2, 3, 4, 6, 9, 12, 18, 36Â
Number of prime factors = 2Input: X = 8Â
Output: 3Â
Some of the numbers having 8 factors are:Â
128 : 1, 2, 4, 8, 16, 32, 64, 128Â
Number of prime factors = 1Â
24 : 1, 2, 3, 4, 6, 8, 12, 24Â
Number of prime factors = 2Â
30 : 1, 2, 3, 5, 6, 10, 15, 30Â
Number of prime factors = 3Â
Â
Approach: The key observation in the problem is, any positive natural number can be represented as product of its prime factors as follows:Â Â
// Number can be represented as product // prime factors as follows// Total number of factors of N can be // defined as followsNumber of Factors = (p+1) * (q+1) * (r+1)..
In the above problem, the number of factors are given which can be used to find the maximum prime factors possible for a number with the given count of factors as follows:Â Â
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
Therefore, the maximum count of prime divisors of a number can have is the count of the prime factors (can be repetitive also) in the factorization of the count of factors of the number.
Below is the implementation of the above approach:Â Â
C++
// C++ implementation to find the// maximum count of the prime factors// by the count of factors of numberÂ
#include <iostream>#include <math.h>Â
using namespace std;Â
// Function to count number// of prime factors of xint 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 Codeint 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 numberimport java.io.*;public 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 numberimport mathÂ
# Function to count number # of prime factors of xdef 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 Codex = 8prime_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 numberusing 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
<script>Â
// Javascript implementation to find the// maximum count of the prime factors// by the count of factors of numberÂ
// Function to count number// of prime factors of xfunction countPrimeFactors(n){Â Â Â Â if (n == 1)Â Â Â Â Â Â Â Â return 0;Â
    // Count variable is    // incremented for every    // prime factor of x    let cnt = 0;         while (n % 2 == 0)    {        cnt++;        n = parseInt(n / 2);    }Â
    // Loop to count the number    // of the prime factors of    // the given number    for(let i = 3; i <= Math.sqrt(n); i += 2)     {        while (n % i == 0)         {            cnt++;            n = parseInt(n / i);        }    }Â
    if (n > 2)        cnt++;Â
    return cnt;}Â
// Driver Codelet x = 8;let prime_factor_cnt = countPrimeFactors(x);Â
document.write(prime_factor_cnt);Â
// This code is contributed by souravmahato348Â
</script> |
3
Â
Time Complexity: Â O(N1/2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



