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
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.
- Smallest number divisible by n and has at-least k trailing zeros
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Count number of trailing zeros in product of array
- Count number of trailing zeros in Binary representation of a number using Bitset
- Smallest number with at least n trailing zeroes in factorial
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Find d to maximize the number of zeros in array c created as c[i] = d*a[i] + b[i]
- Find the smallest number whose digits multiply to a given number n
- Find smallest number K such that K % p = 0 and q % K = 0
- Find smallest permutation of given number
- Given a number, find the next smallest palindrome
- Find the smallest missing number
- Find smallest number n such that n XOR n+1 equals to given k.
- Find the k-th smallest divisor of a natural number N
- Find Nth smallest number that is divisible by 100 exactly K times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.