Number less than equals to N with maximum product of prime factors | Function to find the smallest number having a maximum product of prime factors ; Declare the array arr [ ] ; Iterate from [ 2 , N ] ; If value at index i is 1 , then i is prime and make update array at index for all multiples of i ; Initialise maxValue ; Find number having maximum product of prime factor <= N ; Find the maximum value ; Given Number ; Function call
function maxPrimefactorNum ( N ) { let arr = Array ( N + 1 ) . fill ( 1 ) ; for ( let i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] == 1 ) { for ( let j = i ; j <= N ; j += i ) { arr [ j ] *= i ; } } } let maxValue = 1 ; for ( let i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] > maxValue ) { maxValue = i ; } } return maxValue ; } let N = 20 ; document . write ( maxPrimefactorNum ( N ) ) ;