Count of Binary Digit numbers smaller than N | method returns count of binary digit numbers smaller than N ; queue to store all intermediate binary digit numbers ; binary digits start with 1 ; loop until we have element in queue ; push next binary digit numbers only if current popped element is N ; uncomment below line to print actual number in sorted order ; Driver code
function countOfBinaryNumberLessThanN ( N ) { var q = [ ] ; q . push ( 1 ) ; var cnt = 0 ; var t ; while ( q . length > 0 ) { t = q . pop ( ) ; if ( t <= N ) { cnt ++ ; q . push ( t * 10 ) ; q . push ( t * 10 + 1 ) ; } } return cnt ; } var N = 200 ; document . write ( countOfBinaryNumberLessThanN ( N ) + " " ) ;