Check if each element of the given array is the product of exactly K prime numbers | Python3 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 ; Driver Code ; first construct the sieve
MAX = 1000000 NEW_LINE Sieve = [ 0 ] * ( MAX + 1 ) NEW_LINE def constructSieve ( ) : NEW_LINE INDENT for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( Sieve [ i ] == 0 ) : NEW_LINE INDENT for j in range ( 2 * i , MAX + 1 , i ) : NEW_LINE INDENT temp = j ; NEW_LINE while ( temp > 1 and temp % i == 0 ) : NEW_LINE INDENT Sieve [ j ] += 1 ; NEW_LINE temp = temp // i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def checkElements ( A , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( Sieve [ A [ i ] ] == k ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT constructSieve ( ) ; NEW_LINE k = 3 ; NEW_LINE A = [ 12 , 36 , 42 , 72 ] ; NEW_LINE n = len ( A ) ; NEW_LINE checkElements ( A , n , k ) ; NEW_LINE DEDENT