- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the smallest number X such that X! contains at least Y trailing zeros in C++

We have to take a number Y, we will find smallest number X, such that X! contains at least Y number of training zeros. For example, if Y = 2, then the value of X = 10. As X! = 3228800. It has Y number of zeros.

We can solve this using binary search. The number of trailing zeros in N! is given by the count of the factors 5 in N!. X can be found using binary search in range [0, 5*Y]

## Example

#include<iostream> using namespace std; int factorCount(int n, int X) { if (X < n) return 0; return (X / n + factorCount(n, X / n)); } int findX(int Y) { int left = 0, right = 5 * Y; int N = 0; while (left <= right) { int mid = (right + left) / 2; if (factorCount(5, mid) < Y) { left = mid + 1; }else { N = mid; right = mid - 1; } } return N; } int main() { int Y = 4; cout << "Smallest value of X: " << findX(Y); }

## Output

Smallest value of X: 20

- Related Questions & Answers
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Find smallest values of x and y such that ax – by = 0 in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Get at least x number of rows in MySQL?
- Find larger of x^y and y^x in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Count of sub-strings that contain character X at least once in C++
- Maximum average of a subarray of size of at least X and at most Y in C++

Advertisements