The Trailing Zeros
MediumAcc. 88.2%
+25 XP 10
Counting the Void
Trailing zeros in a factorial are caused by pairs of 2 and 5. Since factorials always have more 2s than 5s, we only need to count how many factors of 5 exist between 1 and $N$.
The Assignment
Your function receives a parameter num.
- Initialize
countto 0. - Use a
whileloop that continues as long asnum / 5is at least 1. - In each step:
- Divide
numby 5 and round down (Math.floor). - Add this result to your
count.
- Divide
- After the loop, print the final
count.
01EXAMPLE 1
Input
num = 25Output
6Explanation: 25! has 6 trailing zeros (5, 10, 15, 20 are 4, plus 25 adds two 5s).
Constraints
- Do not calculate the actual factorial (it will overflow).
- Use the efficient division-by-5 loop.
MathLoops
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
num25
Expected Output
6
Click RUN to test your solution