Given two integer N or M find the number of zero’s trailing in product of factorials (N!*M!)?
Input : N = 4, M = 5 Output : 1 Explanation : 4! = 24, 5! = 120 Product has only 1 trailing 0. Input : N = 127!, M = 57! Output : 44
As discussed in number of zeros in N! can be calculated by recursively dividing N by 5 and adding up the quotients.
For example if N = 127, then
Number of 0 in 127! = 127/5 + 127/25 + 127/125 + 127/625
= 25 + 5 + 1 + 0
Number of 0s in N! = 31. Similarly, for M we can calculate and add both of them.
So, by above we can conclude that number of zeroes in N!*M! Is equal to sum of number of zeroes in N! and M!.
f(N) = floor(N/5) + floor(N/5^2) + … floor(N/5^3) + …
f(M) = floor(x/5) + floor(M/5^2) + … floor(M/5^3) + …
Then answer is f(N)+f(M)
- Count number of trailing zeros in product of array
- Product of first N factorials
- Queries for the product of first N factorials
- Minimum number of Factorials whose sum is equal to N
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if a given number divides the sum of the factorials of its digits
- Number of trailing zeros in N * (N - 2) * (N - 4)*....
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Smallest number divisible by n and has at-least k trailing zeros
- Smallest number with at least n trailing zeroes in factorial
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Number of trailing zeroes in base 16 representation of N!
- Number of trailing zeroes in base B representation of N!
- Count trailing zeroes in factorial of a number
- Largest number with maximum trailing nines which is less than N and greater than N-D
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t