Find the array element from indices not divisible by K having largest composite product of digits | Python3 program to implement the above approach ; Function to check if a number is a composite number or not ; Corner cases ; Check if number is divisible by 2 or 3 ; Check if number is a multiple of any other prime number ; Function to calculate the product of digits of a number ; Stores the product of digits ; Extract digits of a number ; Calculate product of digits ; Function to check if the product of digits of a number is a composite number or not ; Stores product of digits ; If product of digits is equal to 1 ; If product of digits is not prime ; Function to find the number with largest composite product of digits from the indices not divisible by k from the given array ; Traverse the array ; If index is divisible by k ; Check if product of digits is a composite number or not ; Sort the products ; Driver Code
from math import ceil , sqrt NEW_LINE def isComposite ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 5 , ceil ( sqrt ( n ) ) , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def digitProduct ( number ) : NEW_LINE INDENT product = 1 NEW_LINE while ( number > 0 ) : NEW_LINE INDENT product *= ( number % 10 ) NEW_LINE number //= 10 NEW_LINE DEDENT return product NEW_LINE DEDENT def compositedigitProduct ( num ) : NEW_LINE INDENT res = digitProduct ( num ) NEW_LINE if ( res == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( isComposite ( res ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def largestCompositeDigitProduct ( a , n , k ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( i % k ) == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( compositedigitProduct ( a [ i ] ) ) : NEW_LINE INDENT b = digitProduct ( a [ i ] ) NEW_LINE pq . append ( [ b , a [ i ] ] ) NEW_LINE DEDENT DEDENT pq = sorted ( pq ) NEW_LINE return pq [ - 1 ] [ 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 233 , 144 , 89 , 71 , 13 , 21 , 11 , 34 , 55 , 23 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE ans = largestCompositeDigitProduct ( arr , n , k ) NEW_LINE print ( ans ) NEW_LINE DEDENT