The Wayback Machine - https://web.archive.org/web/20240821041505/https://www.geeksforgeeks.org/maximum-number-of-prime-factors-a-number-can-have-with-exactly-x-factors/
Open In App

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

Last Updated : 13 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 positive natural number can be represented as product of its prime factors as follows:  

// Number can be represented as product
// prime factors as follows

N = a^p * b^q * c^r ..// 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 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
import 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 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

<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 x
function 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 Code
let x = 8;
let prime_factor_cnt = countPrimeFactors(x);
 
document.write(prime_factor_cnt);
 
// This code is contributed by souravmahato348
 
</script>

                    

Output: 
3

 

Time Complexity:  O(N1/2)

Auxiliary Space: O(1)



Similar Reads

Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime fa
13 min read
Count numbers in a given range having prime and non-prime digits at prime and non-prime positions respectively
Given two integers L and R, the task is to find the count of numbers in the range [L, R] having prime digits at prime positions and non-prime digits at non-prime positions. Examples: Input: L = 5, R = 22 Output: 7Explanation: The numbers 6, 8, 9, 12, 13, 15, and 17 have prime digits at prime positions and non-prime digits at non-prime positions. In
15 min read
Maximum possible prime divisors that can exist in numbers having exactly N divisors
Given an integer N which denotes the number of divisors of any number, the task is to find the maximum prime divisors that are possible in number having N divisors. Examples: Input: N = 4 Output: 2 Input: N = 8 Output: 3 Naive Approach: In this approach, the idea is to generate all the numbers having exactly N divisors and check for the maximum num
11 min read
Check if there exists a number with X factors out of which exactly K are prime
Given two integers X and K, the task is to determine whether there exists a number that has exactly X factors out of which K is prime.Examples: Input: X = 8, K = 1 Output: Yes Explanation: The number is 128 Factors of 128 = {1, 2, 4, 8, 16, 32, 64, 128} which are 8 in count = X Among these, only 2 is prime. Therefore count of prime factor = 1 = KIn
15+ min read
First element of every K sets having consecutive elements with exactly K prime factors less than N
Given two integers N and K, the task is to find the first element for every set of K consecutive elements which have exactly K prime factors and are less than N. Examples: Input: N = 30, K = 2 Output: 14 20 21 Explanation: Numbers which have prime factors equals to 2 less than 30 = { 14, 15, 18, 20, 21, 22, 24, 26, 28 }. In the above examples, for
11 min read
Check if a number can be expressed as a product of exactly K prime divisors
Given an integer N, the task is to check if it can be expressed as a product of exactly K prime divisors. Examples: Input: N = 12, K = 3 Output: Yes Explanation: 12 can be expressed as product of 2×2×3. Input: N = 14, K = 3 Output: No Explanation: 14 can be only expressed as product of 2×7. Approach:To solve the problem mentioned above we are given
6 min read
Count numbers in a given range whose count of prime factors is a Prime Number
Given a 2D array Q[][] of size N * 2 representing queries of the form {L, R}. For each query, the task is to print the count of numbers in the range [L, R] with a count of prime factors equal to a prime number. Examples: Input: Q[][] = {{4, 8}, {30, 32}} Output: 3 2 Explanation: Query 1: Prime factors of 4 = {2, 2} and count of prime factors = 2 Pr
15 min read
Maximum sum of K-length subarray with maximum count of distinct prime factors
Given an array arr[] consisting of N positive integers and an integer K, the task is to find the maximum sum of array elements in a subarray having maximum sum of distinct prime factors in each K-length subarray. Note: If there are multiple answers then print the sum of the original subarray having maximum sum. Examples: Input: arr[] = {1, 4, 2, 10
11 min read
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers
Given a number N (N &gt; 6), the task is to print the prime factorization of a number Z, where Z is the product of all numbers ≤ N that are even and can be expressed as the product of two distinct prime numbers. Example: Input: N = 6Output: 2→1 3→1Explanation: 6 is the only number ≤ N, which is even and a product of two distinct prime numbers (2 an
7 min read
Number which has the maximum number of distinct prime factors in the range M to N
Given two numbers M and N. The task is to print the number which has the maximum number of distinct prime factors of numbers in range M and N. If there exist multiple numbers, print the smallest one. Examples: Input: a=4, b=10 Output: 6 Number of distinct Prime Factors of 4 is 1 Number of distinct Prime Factors of 5 is 1 Number of distinct Prime Fa
10 min read
Number with maximum number of prime factors
Given an integer N. The task is to find a number that is smaller than or equal to N and has maximum prime factors. In case there are two or more numbers with the same maximum number of prime factors, find the smallest of all.Examples: Input : N = 10Output : 6Number of prime factor of:1 : 02 : 13 : 14 : 15 : 16 : 27 : 18 : 19 : 110 : 26 and 10 have
15+ min read
Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
Given an integer k and a tree with n nodes. The task is to count the number of distinct pairs of vertices that have a distance of exactly k. Examples: Input: k = 2 Output: 4Input: k = 3 Output: 2 Approach: This problem can be solved using dynamic programming. For every vertex v of the tree, we calculate values d[v][lev] (0 &lt;= lev &lt;= k). This
8 min read
Minimum number of additional Trees to have exactly n-leaf nodes
Given a tree with only one node. We want a tree with exactly n-leaf nodes. We have exactly k trees consisting of 1, 2, ..., and k leaf nodes. For example, we had k additional trees. The only operation we can do now is to superimpose the root of one of the additional trees onto any leave of the original tree. Determine the minimum number of addition
7 min read
Check if a prime number can be expressed as sum of two Prime Numbers
Given a prime number N. The task is to check if it is possible to express N as the sum of two separate prime numbers.Note: The range of N is less than 108. Examples: Input: N = 13Output: YesExplanation: The number 13 can be written as 11 + 2, here 11 and 2 are both prime.Input: N = 11Output: NoRecommended: Please try your approach on {IDE} first, b
13 min read
Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
Given a number N which is the number of nodes in a graph, the task is to find the maximum number of edges that N-vertex graph can have such that graph is triangle-free (which means there should not be any three edges A, B, C in the graph such that A is connected to B, B is connected to C and C is connected to A). The graph cannot contain a self-loo
4 min read
Number less than equals to N with maximum product of prime factors
Given a number N, the task is to find the number which is less than or equals to N whose product of prime factors is maximum.Note: If there is more than one number whose maximum product is equal, then print the smallest number of them.Examples: Input: N = 12 Output: 11 Explanation: Product of prime factor of all numbers before N: 2 = 2 3 = 3 4 = 2
7 min read
Maximum number of unique prime factors
Given a number N, find the maximum number of unique prime factors any number can have in range [1, N].Examples: Input : N = 500 Output : 4 The maximum number of prime factors for any number in [1, 500] is 4. A number in range that has 4 prime factors is 210 (2 x 3 x 5 x 7) Input : N = 3 Output : 1 Input : N = 5000 Output : 5 Recommended PracticeMax
10 min read
Build Array of length N such that exactly X Subarrays have positive sums
Given integers N and X, the task is to construct an array of size N such that exactly X subarrays have positive sums and other subarrays have negative sums. Note: If multiple subarrays are possible, print any of them. Examples: Input: N = 3, X = 2Output: 2 -1 -2Explanation: [0, 0] and [0, 1] subarrays have positive sums while other subarrays have n
9 min read
Create Array following condition such that exactly M prefixes have positive Sum
You are given two integers N and M (M ≤ N), the task is to create an array of length N following the below-given conditions. |Ai| = i, Formally, A[i] will be either -i or i.Exactly M prefix arrays should be there, Such that sum of each prefix array should be greater than zero. Note: If there are multiple arrangements, then output any valid arrangem
10 min read
Sub-strings having exactly k characters that have ASCII value greater than p
Given a string 'str', two integers 'k' and 'p'. The task is to count all the sub-strings of 'str' having exactly 'k' characters that have ASCII values greater than 'p'. Examples: Input: str = "abcd", k=2, p=98 Output: 3 Only the characters 'c' and 'd' have ASCII values greater than 98, And, the sub-strings containing them are "abcd", "bcd" and "cd"
7 min read
Check whether a number has exactly three distinct factors or not
Given a positive integer n(1 &lt;= n &lt;= 1018). Check whether a number has exactly three distinct factors or not. Print "Yes" if it has otherwise "No". Examples : Input : 9Output: YesExplanationNumber 9 has exactly three factors:1, 3, 9, hence answer is 'Yes'Input : 10Output : NoRecommended PracticeThree distinct factorsTry It!Simple approach is
10 min read
Queries to find whether a number has exactly four distinct factors or not
Given a positive integers 'q' and 'n'. For each query 'q' find whether a number 'n' have exactly four distinct divisors or not. If the number have exactly four divisors then print 'Yes' else 'No'.1 &lt;= q, n &lt;= 106 Input: 2 10 12 Output: Yes No Explanation: For 1st query, n = 10 has exactly four divisor i.e., 1, 2, 5, 10. For 2nd query, n = 12
11 min read
Count prime numbers up to N that can be represented as a sum of two prime numbers
Given a positive integer N, the task is to find the count of prime numbers less than or equal to N that can be represented as a sum of two prime numbers. Examples: Input: N = 6Output: 1Explanation:5 is the only prime number over the range [1, 6] that can be represented as sum of 2 prime numbers i.e., 5 = 2 + 3, where 2, 3 and 5 are all primes.There
15+ min read
Count prime numbers that can be expressed as sum of consecutive prime numbers
Given an integer N, the task is to find the number of prime numbers up to N that can be expressed as a sum of consecutive primes. Examples: Input: N = 45Output: 3Explanation:Below are the prime numbers up to 45 that can be expressed as sum of consecutive prime numbers: 5 = 2 + 317 = 2 + 3 + 5 + 741 = 2 + 3 + 5 + 7 + 11 + 13Therefore, the count is 3
8 min read
Find the row whose product has maximum count of prime factors
Given a matrix of size N x M, the task is to print the elements of the row whose product has a maximum count of prime factors.Examples: Input: arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output: 7 8 9 Explanation: Row 1: (1, 2, 3) has product 6 and it has 2 prime factors. Row 2: (4, 5, 6) has product 120 and it has 3 prime factors. Row 3: (7, 8, 9
11 min read
Maximum Length of Sequence of Sums of prime factors generated by the given operations
Given two integers N and M, the task is to perform the following operations: For every value in the range [N, M], calculate the sum of its prime factors followed by the sum of prime factors of that sum and so on.Generate the above sequence for each array element and calculate the length of the sequence Illustration N = 8 Prime factors = {2, 2, 2}.
12 min read
Maximum distinct prime factors of elements in a K-length subarray
Given an array arr[] of N positive integers and an integer K, the task is to find the maximum distinct prime factors in a subarray of length K. Examples: Input: arr[] = {5, 9, 14, 6, 10, 77}, K=3Output: 5Explanation: The sub-array of length 3 with maximum distinct prime factors is 6, 10, 77 and prime factors are 2, 3, 5, 7, 11. Input: arr[] = {4, 2
12 min read
Number of factors of very large number N modulo M where M is any prime number
Given a large number N, the task is to find the total number of factors of the number N modulo M where M is any prime number. Examples: Input: N = 9699690, M = 17 Output: 1 Explanation: Total Number of factors of 9699690 is 256 and (256 % 17) = 1Input: N = 193748576239475639, M = 9 Output: 8 Explanation: Total Number of factors of 9699690 is 256 an
8 min read
Sum of nodes of Linked List whose contains values with exactly three factors
Given a singly linked list containing N nodes, the task is to find the sum of all possible nodes of the list which contains value with exactly three distinct factors. Examples: Input: 1 -&gt; 2 -&gt; 4 -&gt; 5 Output: 4 Explanation: Factors of 2 are {1, 2} Factors of 3 are {1, 3} Factors of 4 are {1, 2, 4} Factors of 5 are {1, 5} Since only 4 conta
13 min read
Count numbers from a given range having exactly 5 distinct factors
Given two integers L and R, the task is to calculate the count of numbers from the range [L, R] having exactly 5 distinct positive factors. Examples: Input: L = 1, R= 100 Output: 2 Explanation: The only two numbers in the range [1, 100] having exactly 5 distinct factors are 16 and 81. Factors of 16 are {1, 2, 4, 8, 16}. Factors of 81 are {1, 3, 9,
8 min read
three90RightbarBannerImg