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 number of prime divisors. Below are the steps:
- Define a function is_prime(num) that takes an integer as input and returns True if it is prime, and False otherwise.
- Check if the number is less than 2, in which case it is not prime.
- Use a loop to check if the number is divisible by any integer from 2 up to its square root.
- If it is divisible by any integer, return False.
- If the loop completes without finding a divisor, return True.
Implementation:
Python3
def is_prime(num):
if num < 2 :
return False
for i in range ( 2 , int (num * * 0.5 ) + 1 ):
if num % i = = 0 :
return False
return True
def count_primes(num):
count = 0
for i in range ( 2 , int (num * * 0.5 ) + 1 ):
if num % i = = 0 :
if is_prime(i):
count + = 1
if is_prime(num / / i):
count + = 1
return count
def max_prime_divisors_brute_force(n):
max_primes = 0
max_num = 0
for num in range ( 2 , pow ( 10 , n)):
if len ([i for i in range ( 1 , num + 1 ) if num % i = = 0 ]) = = n:
prime_count = count_primes(num)
if prime_count > max_primes:
max_primes = prime_count
max_num = num
return max_primes
n = 4
result = max_prime_divisors_brute_force(n)
print (result)
|
Time Complexity: O(N2 * log(N))
Space Complexity: O(N)
Approach: The idea is to find the prime factorization of the number N, then the sum of the powers of the prime divisors is the maximum possible prime divisors of a number can have with N divisors.
For Example:
Let the number of divisors of number be 4,
Then the possible numbers can be 6, 10, 15,...
Divisors of 6 = 1, 2, 3, 6
Total number of prime-divisors = 2 (2, 3)
Prime Factorization of 4 = 22
Sum of powers of prime factors = 2
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
#define ll long long int
void findMaxPrimeDivisor( int n){
int max_possible_prime = 0;
while (n % 2 == 0) {
max_possible_prime++;
n = n / 2;
}
for ( int i = 3; i * i <= n; i = i + 2) {
while (n % i == 0) {
max_possible_prime++;
n = n / i;
}
}
if (n > 2) {
max_possible_prime++;
}
cout << max_possible_prime << "\n" ;
}
int main()
{
int n = 4;
findMaxPrimeDivisor(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMaxPrimeDivisor( int n)
{
int max_possible_prime = 0 ;
while (n % 2 == 0 )
{
max_possible_prime++;
n = n / 2 ;
}
for ( int i = 3 ; i * i <= n; i = i + 2 )
{
while (n % i == 0 )
{
max_possible_prime++;
n = n / i;
}
}
if (n > 2 )
{
max_possible_prime++;
}
System.out.print(max_possible_prime + "\n" );
}
public static void main(String[] args)
{
int n = 4 ;
findMaxPrimeDivisor(n);
}
}
|
Python3
def findMaxPrimeDivisor(n):
max_possible_prime = 0
while (n % 2 = = 0 ):
max_possible_prime + = 1
n = n / / 2
i = 3
while (i * i < = n):
while (n % i = = 0 ):
max_possible_prime + = 1
n = n / / i
i = i + 2
if (n > 2 ):
max_possible_prime + = 1
print (max_possible_prime)
n = 4
findMaxPrimeDivisor(n)
|
C#
using System;
class GFG{
static void findMaxPrimeDivisor( int n)
{
int max_possible_prime = 0;
while (n % 2 == 0)
{
max_possible_prime++;
n = n / 2;
}
for ( int i = 3; i * i <= n; i = i + 2)
{
while (n % i == 0)
{
max_possible_prime++;
n = n / i;
}
}
if (n > 2)
{
max_possible_prime++;
}
Console.Write(max_possible_prime + "\n" );
}
public static void Main(String[] args)
{
int n = 4;
findMaxPrimeDivisor(n);
}
}
|
Javascript
<script>
function findMaxPrimeDivisor(n)
{
let max_possible_prime = 0;
while (n % 2 == 0)
{
max_possible_prime++;
n = Math.floor(n / 2);
}
for (let i = 3; i * i <= n; i = i + 2)
{
while (n % i == 0)
{
max_possible_prime++;
n = Math.floor(n / i);
}
}
if (n > 2)
{
max_possible_prime++;
}
document.write(max_possible_prime + "\n" );
}
let n = 4;
findMaxPrimeDivisor(n);
</script>
|
Time Complexity: O(sqrt(N) * logN )
Auxiliary Space: O(1)