Count array elements whose count of divisors is a prime number | Function to count the array elements whose count of divisors is prime ; Stores the maximum element ; Find the maximum element ; Store if i - th element is prime ( 0 ) or non - prime ( 1 ) ; Base Case ; If i is a prime number ; Mark all multiples of i as non - prime ; Stores the count of divisors ; Base Case ; Iterate to count factors ; Stores the count of array elements whose count of divisors is a prime number ; Traverse the array arr [ ] ; If count of divisors is prime ; Return the resultant count ; Driver Code
function primeDivisors ( arr , N ) { var K = arr [ 0 ] ; var i , j ; for ( i = 1 ; i < N ; i ++ ) { K = Math . max ( K , arr [ i ] ) ; } var prime = Array ( K + 1 ) . fill ( 0 ) ; prime [ 0 ] = 1 ; prime [ 1 ] = 1 ; for ( i = 2 ; i < K + 1 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( j = 2 * i ; j < K + 1 ; j += i ) { prime [ j ] = 1 ; } } } var factor = Array ( K + 1 ) . fill ( 0 ) ; factor [ 0 ] = 0 ; factor [ 1 ] = 1 ; for ( i = 2 ; i < K + 1 ; i ++ ) { factor [ i ] += 1 ; for ( j = i ; j < K + 1 ; j += i ) { factor [ j ] += 1 ; } } var count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( prime [ factor [ arr [ i ] ] ] == 0 ) count ++ ; } return count ; } var arr = [ 10 , 13 , 17 , 25 ] ; var N = arr . length ; document . write ( primeDivisors ( arr , N ) ) ;