Queries to count numbers from given range which are divisible by all its digits | Javascript program to implement the above approach ; 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
const Max = 1000005 ; function CheckDivByAllDigits ( number ) { let n = number ; while ( n > 0 ) { if ( n % 10 ) if ( number % ( n % 10 ) ) { return false ; } n = parseInt ( n / 10 ) ; } return true ; } function cntNumInRang ( arr , N ) { let prefCntDiv = new Array ( Max ) . fill ( 0 ) ; for ( let i = 1 ; i <= Max ; i ++ ) { prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ( CheckDivByAllDigits ( i ) ) ; } for ( let i = 0 ; i < N ; i ++ ) document . write ( ( prefCntDiv [ arr [ i ] [ 1 ] ] - prefCntDiv [ arr [ i ] [ 0 ] - 1 ] ) + " " ) ; } let arr = [ [ 1 , 5 ] , [ 12 , 14 ] ] ; let N = arr . length ; cntNumInRang ( arr , N ) ;