Check if each element of the given array is the product of exactly K prime numbers | Javascript program to check if each element of the given array is a product of exactly K prime factors ; initialise the global sieve array ; Function to generate Sieve ; NOTE : k value is necessarily more than 1 hence , 0 , 1 and any prime number cannot be represented as product of two or more prime numbers ; Function to check if each number of array satisfies the given condition ; first construct the sieve
const MAX = 1000000 ; let Sieve = new Array ( MAX ) . fill ( 0 ) ; function constructSieve ( ) { for ( let i = 2 ; i <= MAX ; i ++ ) { if ( Sieve [ i ] == 0 ) { for ( let j = 2 * i ; j <= MAX ; j += i ) { let temp = j ; while ( temp > 1 && temp % i == 0 ) { Sieve [ j ] ++ ; temp = parseInt ( temp / i ) ; } } } } } function checkElements ( A , n , k ) { for ( let i = 0 ; i < n ; i ++ ) { if ( Sieve [ A [ i ] ] == k ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } constructSieve ( ) ; let k = 3 ; let A = [ 12 , 36 , 42 , 72 ] ; let n = A . length ; checkElements ( A , n , k ) ;