Count array elements whose product of digits is a Composite Number | Python3 program for the above approach ; Function to generate prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to calculate the product of digits of the given number ; Stores the product of digits ; Extract digits and add to the sum ; Return the product of its digits ; Function to print number of distinct values with digit product as composite ; Initialize set ; Initialize boolean array ; Pre - compute primes ; Traverse array ; of the current array element ; If Product of digits is less than or equal to 1 ; If Product of digits is not a prime ; Print the answer ; Driver Code ; Given array ; Given size ; Function call
N = 100005 NEW_LINE from math import sqrt NEW_LINE def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( p_size ) ) , 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def digitProduct ( number ) : NEW_LINE INDENT res = 1 NEW_LINE while ( number > 0 ) : NEW_LINE INDENT res *= ( number % 10 ) NEW_LINE number //= 10 NEW_LINE DEDENT return res NEW_LINE DEDENT def DistinctCompositeDigitProduct ( arr , n ) : NEW_LINE INDENT output = set ( ) NEW_LINE prime = [ True for i in range ( N + 1 ) ] NEW_LINE SieveOfEratosthenes ( prime , N ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = digitProduct ( arr [ i ] ) NEW_LINE if ( ans <= 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( prime [ ans ] == False ) : NEW_LINE INDENT output . add ( ans ) NEW_LINE DEDENT DEDENT print ( len ( output ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 ] NEW_LINE n = len ( arr ) NEW_LINE DistinctCompositeDigitProduct ( arr , n ) NEW_LINE DEDENT