Queries to count numbers from given range which are divisible by all its digits | Function to check if a number is divisible by all of its non - zero digits or not ; Stores the number ; Iterate over the digits of the numbers ; If digit of number is non - zero ; If number is not divisible by its current digit ; Update n ; Function to count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Stores count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Iterate over the range [ 1 , Max ] ; Update ; Traverse the array , arr [ ] ; Driver Code
def CheckDivByAllDigits ( number ) : NEW_LINE INDENT n = number NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( n % 10 ) : NEW_LINE INDENT if ( number % ( n % 10 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n //= 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def cntNumInRang ( arr , N ) : NEW_LINE INDENT global Max NEW_LINE prefCntDiv = [ 0 ] * Max NEW_LINE for i in range ( 1 , Max ) : NEW_LINE INDENT prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ( CheckDivByAllDigits ( i ) ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( prefCntDiv [ arr [ i ] [ 1 ] ] - prefCntDiv [ arr [ i ] [ 0 ] - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 5 ] , [ 12 , 14 ] ] NEW_LINE Max = 1000005 NEW_LINE N = len ( arr ) NEW_LINE cntNumInRang ( arr , N ) NEW_LINE DEDENT