Count array elements whose product of digits is a Composite Number | javascript 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 ; Stores the product of digits 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 ; Given array ; Given size ; Function call
var N = 100005 ; function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; var p , i ; for ( p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function digitProduct ( number ) { var res = 1 ; while ( number > 0 ) { res *= ( number % 10 ) ; number = parseInt ( number / 10 ) ; } return res ; } function DistinctCompositeDigitProduct ( arr , n ) { var output = new Set ( ) ; var prime = Array ( N + 1 ) . fill ( true ) ; SieveOfEratosthenes ( prime , N ) ; var i ; for ( i = 0 ; i < n ; i ++ ) { var ans = digitProduct ( arr [ i ] ) ; if ( ans <= 1 ) { continue ; } if ( prime [ ans ] == false ) { output . add ( ans ) ; } } document . write ( output . size ) ; } var arr = [ 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 ] ; var n = arr . length ; DistinctCompositeDigitProduct ( arr , n ) ;