Given an integer N, the task is to find the number of trailing zeros in the decimal notation of f(N) where f(N) = 1 if N < 2 and f(N) = N * f(N – 2) if N ≥ 2
Input: N = 12
f(12) = 12 * 10 * 8 * 6 * 4 * 2 = 46080
Input: N = 7
Approach: The number of trailing zeros when f(N) is expressed in decimal notation is the number of times f(N) is divisible by 2 and the number of times f(N) is divisible by 5. There are two cases:
- When N is odd then f(N) is the product of some odd numbers, so it does not break at 2. So the answer is always 0.
- When N is even then f(N) can be represented as 2 (1 * 2 * 3 * …. * N/2). The number of times f(N) is divisible by 2 is greater than the number of times divisible by 5, so only consider the number of times divisible by 5. Now, this problem is similar to count trailing zeroes in factorial of a number.
Below is the implementation of the above approach:
- 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
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Count number of trailing zeros in product of array
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Trailing number of 0s in product of two factorials
- Count trailing zeroes in factorial of a number
- Number of trailing zeroes in base B representation of N!
- Smallest number with at least n trailing zeroes in factorial
- Number of trailing zeroes in base 16 representation of N!
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Numbers whose factorials end with n zeros
- Count numbers having N 0's and and M 1's with no leading zeros
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.