Find the row whose product has maximum count of prime factors | JavaScript implementation to find the row whose product has maximum number of prime factors ; function for SieveOfEratosthenes ; Create a boolean array " isPrime [ 0 . . N ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is not a prime , else true . ; check if isPrime [ p ] is not changed ; Update all multiples of p ; Print all isPrime numbers ; function to display the answer ; function to Count the row number of divisors in particular row multiplication ; Find count of occurrences of each prime factor ; Compute count of all divisors ; Update row number if factors of this row is max ; Clearing map to store prime factors for next row ; Driver code
let N = 3 let M = 5 let Large = 1e6 ; let prime = new Array ( ) ; function SieveOfEratosthenes ( ) { let isPrime = new Array ( ) ; for ( let i = 0 ; i < Large + 1 ; i ++ ) { isPrime . push ( [ ] ) } isPrime . fill ( true ) ; for ( let p = 2 ; p * p <= Large ; p ++ ) { if ( isPrime [ p ] == true ) { for ( let i = p * 2 ; i <= Large ; i += p ) isPrime [ i ] = false ; } } for ( let p = 2 ; p <= Large ; p ++ ) if ( isPrime [ p ] ) prime . push ( p ) ; } function Display ( arr , row ) { for ( let i = 0 ; i < M ; i ++ ) document . write ( arr [ row ] [ i ] + " " ) ; } function countDivisorsMult ( arr ) { let mp = new Map ( ) ; let row_no = 0 ; let max_factor = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { let no = arr [ i ] [ j ] ; for ( let k = 0 ; k < prime . length ; k ++ ) { while ( no > 1 && no % prime [ k ] == 0 ) { no /= prime [ k ] ; if ( mp . has ( prime [ k ] ) ) { mp . set ( prime [ k ] , mp . get ( prime [ k ] ) + 1 ) } else { mp . set ( prime [ k ] , 1 ) } } if ( no == 1 ) break ; } } let res = 1 ; for ( let it of mp ) { res *= ( it [ 1 ] + 1 ) ; } if ( max_factor < res ) { row_no = i ; max_factor = res ; } mp . clear ( ) ; } Display ( arr , row_no ) ; } let arr = [ [ 1 , 2 , 3 , 10 , 23 ] , [ 4 , 5 , 6 , 7 , 8 ] , [ 7 , 8 , 9 , 15 , 45 ] ] ; SieveOfEratosthenes ( ) ; countDivisorsMult ( arr ) ;