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 Factors of 6 is 2
Number of distinct Prime Factors of 7 is 1
Number of distinct Prime Factors of 8 is 1
Number of distinct Prime Factors of 9 is 1
Number of distinct Prime Factors of 10 is 2
Input: a=100, b=150
Output: 102
The approach is to use Sieve of Eratosthenes. Create a factorCount[] array to store the number of distinct prime factors of a number. While marking the number as prime, increment the count of prime factors in its multiples. In the end, get the maximum number stored in the factorCount[] array which will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumNumberDistinctPrimeRange( int m, int n)
{
long long factorCount[n + 1];
bool prime[n + 1];
for ( int i = 0; i <= n; i++) {
factorCount[i] = 0;
prime[i] = true ;
}
for ( int i = 2; i <= n; i++) {
if (prime[i] == true ) {
factorCount[i] = 1;
for ( int j = i * 2; j <= n; j += i) {
factorCount[j]++;
prime[j] = false ;
}
}
}
int max = factorCount[m];
int num = m;
for ( int i = m; i <= n; i++) {
if (factorCount[i] > max) {
max = factorCount[i];
num = i;
}
}
return num;
}
int main()
{
int m = 4, n = 6;
cout << maximumNumberDistinctPrimeRange(m, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int maximumNumberDistinctPrimeRange( int m,
int n)
{
long factorCount[] = new long [n + 1 ];
boolean prime[] = new boolean [n + 1 ];
for ( int i = 0 ; i <= n; i++)
{
factorCount[i] = 0 ;
prime[i] = true ;
}
for ( int i = 2 ; i <= n; i++)
{
if (prime[i] == true )
{
factorCount[i] = 1 ;
for ( int j = i * 2 ; j <= n; j += i)
{
factorCount[j]++;
prime[j] = false ;
}
}
}
int max = ( int )factorCount[m];
int num = m;
for ( int i = m; i <= n; i++)
{
if (factorCount[i] > max)
{
max = ( int )factorCount[i];
num = i;
}
}
return num;
}
public static void main (String[] args)
{
int m = 4 , n = 6 ;
System.out.println(maximumNumberDistinctPrimeRange(m, n));
}
}
|
Python3
def maximumNumberDistinctPrimeRange(m, n):
factorCount = [ 0 ] * (n + 1 )
prime = [ False ] * (n + 1 )
for i in range (n + 1 ) :
factorCount[i] = 0
prime[i] = True
for i in range ( 2 , n + 1 ) :
if (prime[i] = = True ) :
factorCount[i] = 1
for j in range (i * 2 , n + 1 , i) :
factorCount[j] + = 1
prime[j] = False
max = factorCount[m]
num = m
for i in range (m, n + 1 ) :
if (factorCount[i] > max ) :
max = factorCount[i]
num = i
return num
if __name__ = = "__main__" :
m = 4
n = 6
print (maximumNumberDistinctPrimeRange(m, n))
|
C#
using System;
class GFG
{
static int maximumNumberDistinctPrimeRange( int m,
int n)
{
long []factorCount = new long [n + 1];
bool []prime = new bool [n + 1];
for ( int i = 0; i <= n; i++)
{
factorCount[i] = 0;
prime[i] = true ;
}
for ( int i = 2; i <= n; i++)
{
if (prime[i] == true )
{
factorCount[i] = 1;
for ( int j = i * 2;
j <= n; j += i)
{
factorCount[j]++;
prime[j] = false ;
}
}
}
int max = ( int )factorCount[m];
int num = m;
for ( int i = m; i <= n; i++)
{
if (factorCount[i] > max)
{
max = ( int )factorCount[i];
num = i;
}
}
return num;
}
public static void Main ()
{
int m = 4, n = 6;
Console.WriteLine(
maximumNumberDistinctPrimeRange(m, n));
}
}
|
PHP
<?php
function maximumNumberDistinctPrimeRange( $m , $n )
{
$factorCount = array ();
$prime = array ();
for ( $i = 0; $i <= $n ; $i ++)
{
$factorCount [ $i ] = 0;
$prime [ $i ] = true;
}
for ( $i = 2; $i <= $n ; $i ++)
{
if ( $prime [ $i ] == true)
{
$factorCount [ $i ] = 1;
for ( $j = $i * 2;
$j <= $n ; $j += $i )
{
$factorCount [ $j ]++;
$prime [ $j ] = false;
}
}
}
$max = $factorCount [ $m ];
$num = $m ;
for ( $i = $m ; $i <= $n ; $i ++)
{
if ( $factorCount [ $i ] > $max )
{
$max = $factorCount [ $i ];
$num = $i ;
}
}
return $num ;
}
$m = 4; $n = 6;
echo maximumNumberDistinctPrimeRange( $m , $n );
?>
|
Javascript
<script>
function maximumNumberDistinctPrimeRange(m, n)
{
let factorCount = new Array(n + 1);
let prime = new Array(n + 1);
for (let i = 0; i <= n; i++)
{
factorCount[i] = 0;
prime[i] = true ;
}
for (let i = 2; i <= n; i++)
{
if (prime[i] == true )
{
factorCount[i] = 1;
for (let j = i * 2; j <= n; j += i)
{
factorCount[j]++;
prime[j] = false ;
}
}
}
let max = factorCount[m];
let num = m;
for (let i = m; i <= n; i++)
{
if (factorCount[i] > max)
{
max = factorCount[i];
num = i;
}
}
return num;
}
let m = 4, n = 6;
document.write(maximumNumberDistinctPrimeRange(m, n));
</script>
|