Given a number n. The task is to find the smallest number whose factorial contains at least n trailing zeroes.
Input : n = 1 Output : 5 1!, 2!, 3!, 4! does not contain trailing zero. 5! = 120, which contains one trailing zero. Input : n = 6 Output : 25
In the article for Count trailing zeroes in factorial of a number, we have discussed number of zeroes is equal to number of 5’s in prime factors of x!. We have discussed below formula to count number of 5’s.
Trailing 0s in x! = Count of 5s in prime factors of x! = floor(x/5) + floor(x/25) + floor(x/125) + ....
Let us take few examples to observe pattern
5! has 1 trailing zeroes [All numbers from 6 to 9 have 1 trailing zero] 10! has 2 trailing zeroes [All numbers from 11 to 14 have 2 trailing zeroes] 15! to 19! have 3 trailing zeroes 20! to 24! have 4 trailing zeroes 25! to 29! have 6 trailing zeroes
We can notice that, the minimum value whose factorial contain n trailing zeroes is 5*n.
So, to find minimum value whose factorial contains n trailing zeroes, use binary search on range from 0 to 5*n. And, find the smallest number whose factorial contains n trailing zeroes.
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count trailing zeroes in factorial of a number
- Number of trailing zeroes in base 16 representation of N!
- Number of trailing zeroes in base B representation of N!
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Smallest number divisible by n and has at-least k trailing zeros
- Smallest number S such that N is a factor of S factorial or S!
- Smallest number with at least n digits in factorial
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find the number of zeroes
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Trailing number of 0s in product of two factorials
- Find row with maximum and minimum number of zeroes in given Matrix
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count number of trailing zeros in product of array
- Find the last digit when factorial of A divides factorial of B
Improved By : jit_t