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 = 2

Input: 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 number can be represented into its prime factors as follows:

// Number can be represented in its
// prime factors as follows
N = a^p * b^q * c^r ..

// Number of prime factors can be 
// defined as follows
Number of Factors = (p+1) * (q+1) * (r+1)..

In the above problem, the number of the prime 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:



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

Therefore, the maximum count of prime divisors of a number can have is the count of the prime factors in the factorization of the count of factors of the number.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implmentation 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 x
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 <= 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implmentation 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implmentation 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

chevron_right


Output:

3

competitive-programming-img




My Personal Notes arrow_drop_up

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.