Given an integer Y, find the smallest number X such that X! contains at least Y trailing zeros.

Prerequisites – Count trailing zeroes in factorial of a number

**Examples:**

Input :Y = 2

Output :10

10! = 3628800, which has 2 trailing zeros. 9! = 362880, which has 1 trailing zero. Hence, 10 is the correct answer.

Input :Y = 6

Output :25

25! = 15511210043330985984000000, which has 6 trailing zeros. 24! = 620448401733239439360000, which has 4 trailing zeros. Hence, 25 is the correct answer.

**Approach:** The problem can be easily solved by using **Binary Search**. The number of trailing zeros in N! is given by the count of the factors 5 in N!.Read this article for prerequisites. The countFactor(5, N) function returns the count of factor 5 in N! which is equal to count of trailing zeros in N!. The smallest number X such that X! contains at least Y trailing zeros can be computed quickly by using binary search on a range [0, 5 * Y] using this function.

Below is the implementation of above approach.

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the number ` `// of factors P in X! ` `int` `countFactor(` `int` `P, ` `int` `X) ` `{ ` ` ` `if` `(X < P) ` ` ` `return` `0; ` ` ` ` ` `return` `(X / P + countFactor(P, X / P)); ` `} ` ` ` `// Function to find the smallest X such ` `// that X! contains Y trailing zeros ` `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; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `Y = 10; ` ` ` ` ` `cout << findSmallestX(Y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to count the number ` ` ` `// of factors P in X! ` ` ` `static` `int` `countFactor(` `int` `P, ` `int` `X) ` ` ` `{ ` ` ` `if` `(X < P) ` ` ` `return` `0` `; ` ` ` ` ` `return` `(X / P + countFactor(P, X / P)); ` ` ` `} ` ` ` ` ` `// Function to find the smallest X such ` ` ` `// that X! contains Y trailing zeros ` ` ` `static` `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; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `Y = ` `10` `; ` ` ` ` ` `System.out.println(findSmallestX(Y)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to count the number ` `# of factors P in X! ` `def` `countFactor(P, X): ` ` ` `if` `(X < P): ` ` ` `return` `0` `; ` ` ` ` ` `return` `(X ` `/` `/` `P ` `+` `countFactor(P, X ` `/` `/` `P)); ` ` ` `# Function to find the smallest X such ` `# that X! contains Y trailing zeros ` `def` `findSmallestX(Y): ` ` ` ` ` `low ` `=` `0` `; ` ` ` `high ` `=` `5` `*` `Y; ` ` ` `N ` `=` `0` `; ` ` ` `while` `(low <` `=` `high): ` ` ` `mid ` `=` `(high ` `+` `low) ` `/` `/` `2` `; ` ` ` ` ` `if` `(countFactor(` `5` `, mid) < Y): ` ` ` `low ` `=` `mid ` `+` `1` `; ` ` ` ` ` `else` `: ` ` ` `N ` `=` `mid; ` ` ` `high ` `=` `mid ` `-` `1` `; ` ` ` ` ` `return` `N; ` ` ` `# Driver code ` `Y ` `=` `10` `; ` ` ` `print` `(findSmallestX(Y)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to count the number ` `// of factors P in X! ` `static` `int` `countFactor(` `int` `P, ` `int` `X) ` `{ ` ` ` `if` `(X < P) ` ` ` `return` `0; ` ` ` ` ` `return` `(X / P + countFactor(P, X / P)); ` `} ` ` ` `// Function to find the smallest X such ` `// that X! contains Y trailing zeros ` `static` `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; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `Y = 10; ` ` ` ` ` `System.Console.WriteLine(findSmallestX(Y)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Function to count the number ` `// of factors P in X! ` `function` `countFactor(` `$P` `, ` `$X` `) ` `{ ` ` ` `if` `(` `$X` `< ` `$P` `) ` ` ` `return` `0; ` ` ` ` ` `return` `((int)(` `$X` `/ ` `$P` `) + ` ` ` `countFactor(` `$P` `, (` `$X` `/ ` `$P` `))); ` `} ` ` ` `// Function to find the smallest X such ` `// that X! contains Y trailing zeros ` `function` `findSmallestX(` `$Y` `) ` `{ ` ` ` `$low` `= 0; ` `$high` `= 5 * ` `$Y` `; ` ` ` `$N` `= 0; ` ` ` `while` `(` `$low` `<= ` `$high` `) ` ` ` `{ ` ` ` `$mid` `= (int)((` `$high` `+ ` `$low` `) / 2); ` ` ` ` ` `if` `(countFactor(5, ` `$mid` `) < ` `$Y` `) ` ` ` `{ ` ` ` `$low` `= ` `$mid` `+ 1; ` ` ` `} ` ` ` ` ` `else` ` ` `{ ` ` ` `$N` `= ` `$mid` `; ` ` ` `$high` `= ` `$mid` `- 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$N` `; ` `} ` ` ` `// Driver code ` `$Y` `= 10; ` ` ` `echo` `(findSmallestX(` `$Y` `)); ` ` ` `// This code is contributed by Code_Mech. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

45

## Recommended Posts:

- Smallest number divisible by n and has at-least k trailing zeros
- Smallest number with at least n trailing zeroes in factorial
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of trailing zeros in product of array
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Number of trailing zeros in N * (N - 2) * (N - 4)*....
- Golang Program to Count Trailing Zeros in Factorial of a Number
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Convert a number of length N such that it contains any one digit at least 'K' times
- Minimum number of integers required such that each Segment contains at least one of them
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count trailing zeroes in factorial of a number
- Trailing number of 0s in product of two factorials
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Number of trailing zeroes in base 16 representation of N!
- Number of trailing zeroes in base B representation of N!
- Smallest number with at least n digits in factorial
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Find smallest perfect square number A such that N + A is also a perfect square number
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.