Numbers whose factorials end with n zeros | Function to calculate trailing zeros ; binary search for first number with n trailing zeros ; Print all numbers after low with n trailing zeros . ; Print result ; Driver code
function trailingZeroes ( n ) { var cnt = 0 ; while ( n > 0 ) { n = parseInt ( n / 5 ) ; cnt += n ; } return cnt ; } function binarySearch ( n ) { var low = 0 ; while ( low < high ) { var mid = parseInt ( ( low + high ) / 2 ) ; var count = trailingZeroes ( mid ) ; if ( count < n ) low = mid + 1 ; else high = mid ; } var result = [ ] ; while ( trailingZeroes ( low ) == n ) { result . push ( low ) ; low ++ ; } for ( var i = 0 ; i < result . length ; i ++ ) document . write ( result [ i ] + " " ) ; } var n = 2 ; binarySearch ( n ) ;