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.

  1. Initialize count to 0.
  2. Use a while loop that continues as long as num / 5 is at least 1.
  3. In each step:
    • Divide num by 5 and round down (Math.floor).
    • Add this result to your count.
  4. After the loop, print the final count.

01EXAMPLE 1

Inputnum = 25
Output6

Explanation: 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
JavaScript
Loading...
3 Hidden

Input Arguments

num25

Expected Output

6

Click RUN to test your solution