Count divisors of array multiplication | javascript program to count divisors in array multiplication . ; 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 isPrime , else true . ; If isPrime [ p ] is not changed , then it is a isPrime ; Update all multiples of p ; Print all isPrime numbers ; Returns number of divisors in array multiplication ; Find all prime numbers smaller than the largest element . ; Find counts of occurrences of each prime factor ; Compute count of all divisors using counts prime factors . ; Driver code
function SieveOfEratosthenes ( largest , prime ) { var isPrime = Array ( largest + 1 ) . fill ( true ) ; var p , i ; for ( p = 2 ; p * p <= largest ; p ++ ) { if ( isPrime [ p ] == true ) { for ( i = p * 2 ; i <= largest ; i += p ) isPrime [ i ] = false ; } } for ( p = 2 ; p <= largest ; p ++ ) if ( isPrime [ p ] ) prime . push ( p ) ; } function countDivisorsMult ( arr , n ) { var largest = Math . max . apply ( null , arr ) ; var prime = [ ] ; SieveOfEratosthenes ( largest , prime ) ; var j ; var mp = new Map ( ) ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < prime . length ; j ++ ) { while ( arr [ i ] > 1 && arr [ i ] % prime [ j ] == 0 ) { arr [ i ] /= prime [ j ] ; if ( mp . has ( prime [ j ] ) ) mp . set ( prime [ j ] , mp . get ( prime [ j ] ) + 1 ) ; else mp . set ( prime [ j ] , 1 ) ; } } if ( arr [ i ] != 1 ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; else mp . set ( arr [ i ] , 1 ) ; } } var res = 1 ; for ( const [ key , value ] of mp . entries ( ) ) { res *= ( value + 1 ) ; } return res ; } var arr = [ 2 , 4 , 6 ] ; var n = arr . length ; document . write ( countDivisorsMult ( arr , n ) ) ;