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
Method 1 (brute force):
For each integer from 1 to N, find the number of prime factor of each integer and find the number of maximum unique prime factors.
Method 2 (Better Approach):
Use sieve method to count a number of prime factors of each number less than N. And find the minimum number having maximum count.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxPrimefactorNum( int N)
{
int arr[N + 1];
memset (arr, 0, sizeof (arr));
for ( int i = 2; i * i <= N; i++) {
if (!arr[i])
for ( int j = 2 * i; j <= N; j += i)
arr[j]++;
arr[i] = 1;
}
return *max_element(arr, arr+N);
}
int main()
{
int N = 40;
cout << maxPrimefactorNum(N) << endl;
return 0;
}
|
Java
class GFG
{
static int getMax( int [] Arr)
{
int max = Arr[ 0 ];
for ( int i = 1 ; i < Arr.length; i++)
if (Arr[i] > max)
max = Arr[i];
return max;
}
static int maxPrimefactorNum( int N)
{
int [] arr = new int [N + 1 ];
for ( int i = 2 ; i * i <= N; i++)
{
if (arr[i] == 0 )
for ( int j = 2 * i; j <= N; j += i)
arr[j]++;
arr[i] = 1 ;
}
return getMax(arr);
}
public static void main(String[] args)
{
int N = 40 ;
System.out.println(maxPrimefactorNum(N));
}
}
|
Python3
def maxPrimefactorNum(N):
arr = [ 0 ] * (N + 1 );
i = 2 ;
while (i * i < = N):
if (arr[i] > 0 ):
for j in range ( 2 * i, N + 1 , i):
arr[j] + = 1 ;
i + = 1 ;
arr[i] = 1 ;
return max (arr);
N = 40 ;
print (maxPrimefactorNum(N));
|
C#
using System;
class GFG
{
static int getMax( int [] Arr)
{
int max = Arr[0];
for ( int i = 1; i < Arr.Length; i++)
if (Arr[i] > max)
max = Arr[i];
return max;
}
static int maxPrimefactorNum( int N)
{
int [] arr = new int [N + 1];
for ( int i = 2; i * i <= N; i++)
{
if (arr[i] == 0)
for ( int j = 2 * i;
j <= N; j += i)
arr[j]++;
arr[i] = 1;
}
return getMax(arr);
}
public static void Main()
{
int N = 40;
Console.WriteLine(maxPrimefactorNum(N));
}
}
|
PHP
<?php
function maxPrimefactorNum( $N )
{
$arr = array_fill (0, $N + 1, 0);
for ( $i = 2; $i * $i <= $N ; $i ++)
{
if (! $arr [ $i ])
for ( $j = 2 * $i ; $j <= $N ; $j += $i )
$arr [ $j ]++;
$arr [ $i ] = 1;
}
return max( $arr );
}
$N = 40;
echo maxPrimefactorNum( $N );
?>
|
Javascript
<script>
function getMax(Arr)
{
let max = Arr[0];
for (let i = 1; i < Arr.length; i++)
if (Arr[i] > max)
max = Arr[i];
return max;
}
function maxPrimefactorNum(N)
{
let arr = new Array(N+1).fill(0);
for (let i = 2; i * i <= N; i++)
{
if (arr[i] == 0)
for (let j = 2 * i; j <= N; j += i)
arr[j]++;
arr[i] = 1;
}
return getMax(arr);
}
let N = 40;
document.write(maxPrimefactorNum(N));
</script>
|
Output:
3
Time Complexity: O(n log(log(n)))
Auxiliary Space: O(n)
Method 3 (efficient approach):
Generate all prime numbers before N using Sieve. Now, multiply consecutive prime numbers (starting from first prime number) one after another until the product is less than N. The idea is based on simple fact that the first set of prime numbers can cause maximum unique prime factors.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxPrimefactorNum( int N)
{
if (N < 2)
return 0;
bool arr[N+1];
memset (arr, true , sizeof (arr));
int prod = 1, res = 0;
for ( int p=2; p*p<=N; p++)
{
if (arr[p] == true )
{
for ( int i=p*2; i<=N; i += p)
arr[i] = false ;
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
int main()
{
int N = 500;
cout << maxPrimefactorNum(N) << endl;
return 0;
}
|
Java
class GFG
{
static int maxPrimefactorNum( int N)
{
if (N < 2 )
return 0 ;
boolean [] arr = new boolean [N + 1 ];
int prod = 1 , res = 0 ;
for ( int p = 2 ; p * p <= N; p++)
{
if (arr[p] == false )
{
for ( int i = p * 2 ;
i <= N; i += p)
arr[i] = true ;
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
public static void main(String[] args)
{
int N = 500 ;
System.out.println(maxPrimefactorNum(N));
}
}
|
Python3
def maxPrimefactorNum(N):
if (N < 2 ):
return 0 ;
arr = [ True ] * (N + 1 );
prod = 1 ;
res = 0 ;
p = 2 ;
while (p * p < = N):
if (arr[p] = = True ):
for i in range (p * 2 , N + 1 , p):
arr[i] = False ;
prod * = p;
if (prod > N):
return res;
res + = 1 ;
p + = 1 ;
return res;
N = 500 ;
print (maxPrimefactorNum(N));
|
C#
using System;
class GFG
{
static int maxPrimefactorNum( int N)
{
if (N < 2)
return 0;
bool [] arr = new bool [N + 1];
int prod = 1, res = 0;
for ( int p = 2; p * p <= N; p++)
{
if (arr[p] == false )
{
for ( int i = p * 2;
i <= N; i += p)
arr[i] = true ;
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
public static void Main()
{
int N = 500;
Console.WriteLine(maxPrimefactorNum(N));
}
}
|
PHP
<?php
function maxPrimefactorNum( $N )
{
if ( $N < 2)
return 0;
$arr = array_fill (0, ( $N + 1), true);
$prod = 1;
$res = 0;
for ( $p = 2;
$p * $p <= $N ; $p ++)
{
if ( $arr [ $p ] == true)
{
for ( $i = $p * 2;
$i <= $N ; $i += $p )
$arr [ $i ] = false;
$prod *= $p ;
if ( $prod > $N )
return $res ;
$res ++;
}
}
return $res ;
}
$N = 500;
echo maxPrimefactorNum( $N ) . "\n" ;
?>
|
Javascript
<script>
function maxPrimefactorNum(N)
{
if (N < 2)
return 0;
arr = Array.from({length: N + 1}, (_, i) => false );
var prod = 1, res = 0;
for ( var p = 2; p * p <= N; p++)
{
if (arr[p] == false )
{
for ( var i = p * 2;
i <= N; i += p)
arr[i] = true ;
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
var N = 500;
document.write(maxPrimefactorNum(N));
</script>
|
Output:
4
Time Complexity: O(n log(log n))
Auxiliary Space: O(n)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...