Number which has the maximum number of distinct prime factors in the range M to N | Function to return the maximum number ; Array to store the number of distinct primes ; True if index ' i ' is a prime ; Initializing the number of factors to 0 and ; Used in Sieve ; Condition works only when ' i ' is prime , hence for factors of all prime number , the prime status is changed to false ; Number is prime ; Number of factor of a prime number is 1 ; Incrementing factorCount all the factors of i ; And changing prime status to false ; Initialize the max and num ; Gets the maximum number ; Gets the maximum number ; Driver code ; Calling function
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 ) ) ;