Find prime factors of Array elements whose sum of exponents is divisible by K | To store the smallest prime factor till 10 ^ 5 ; Function to compute smallest prime factor array ; Initialize the spf array first element ; Marking smallest prime factor for every number to be itself ; Separately marking smallest prime factor for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function that finds minimum operation ; Create a spf [ ] array ; Map created to store the unique prime numbers ; To store the result ; To store minimum operations ; To store every unique prime number ; Erase 1 as a key because it is not a prime number ; First Prime Number ; Frequency is divisible by K then insert primeNum in the result [ ] ; Print the elements if it exists ; Given array arr [ ] ; Given K ; Function Call
var spf = Array ( 10001 ) ; function spf_array ( spf ) { spf [ 1 ] = 1 ; for ( var i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( var i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( var i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( var j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } function frequent_prime ( arr , N , K ) { spf_array ( spf ) ; var Hmap = new Map ( ) ; var result = [ ] ; var i = 0 ; var c = 0 ; for ( i = 0 ; i < N ; i ++ ) { var x = arr [ i ] ; while ( x != 1 ) { if ( Hmap . has ( spf [ x ] ) ) Hmap . set ( spf [ x ] , Hmap . get ( spf [ x ] ) + 1 ) else Hmap . set ( spf [ x ] , 1 ) ; x = parseInt ( x / spf [ x ] ) ; } } Hmap . delete ( 1 ) ; Hmap . forEach ( ( value , key ) => { var primeNum = key ; var frequency = value ; if ( frequency % K == 0 ) { result . push ( primeNum ) ; } } ) ; if ( result . length > 0 ) { result . forEach ( it => { document . write ( it + " " ) ; } ) ; } else { document . write ( " " ) ; } } var arr = [ 1 , 4 , 6 ] ; var K = 1 ; var N = arr . length ; frequent_prime ( arr , N , K ) ;