Count all N digit numbers whose digits are multiple of X | Function to calculate x ^ n using binary - exponentiation ; Stores the resultant power ; Stores the value of x ^ ( n / 2 ) ; Function to count all N - digit numbers whose digits are multiples of x ; Count all digits which are multiples of x ; Check if current number is a multiple of X ; Increase count of multiples ; Check if it 's a 1 digit number ; Count the total numbers ; Return the total numbers ; Given N and X ; Function Call
function power ( x , n ) { let temp ; if ( n == 0 ) return 1 ; temp = power ( x , parseInt ( n / 2 ) ) ; if ( n % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } function count_Total_Numbers ( n , x ) { let total , multiples = 0 ; for ( let i = 0 ; i < 10 ; i ++ ) { if ( i % x == 0 ) multiples ++ ; } if ( n == 1 ) return multiples ; total = ( multiples - 1 ) * power ( multiples , n - 1 ) ; return total ; } let N = 1 , X = 3 ; document . write ( count_Total_Numbers ( N , X ) ) ;