Find the smallest number X such that X ! contains at least Y trailing zeros . | C ++ implementation of the approach ; Function to count the number of factors P in X ! ; Function to find the smallest X such that X ! contains Y trailing zeros ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFactor ( int P , int X ) { if ( X < P ) return 0 ; return ( X / P + countFactor ( P , X / P ) ) ; } int findSmallestX ( int Y ) { int low = 0 , high = 5 * Y ; int N = 0 ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( countFactor ( 5 , mid ) < Y ) { low = mid + 1 ; } else { N = mid ; high = mid - 1 ; } } return N ; } int main ( ) { int Y = 10 ; cout << findSmallestX ( Y ) ; return 0 ; }