Count numbers up to N having digit D in its octal representation | Function to count the numbers in given range whose octal representation contains atleast digit , d ; Store count of numbers up to n whose octal representation contains digit d ; Iterate over the range [ 1 , n ] ; Calculate digit of i in octal representation ; Check if octal representation of x contains digit d ; Update total ; Update x ; Prlet the answer ; Given N and D ; Counts and prlets numbers up to N having D as a digit in its octal representation
function countNumbers ( n , d ) { let total = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let x = i ; while ( x > 0 ) { if ( x % 8 == d ) { total ++ ; break ; } x = x / 8 ; } } document . write ( total ) ; } let n = 20 , d = 7 ; countNumbers ( n , d ) ;