Editor

Trailing Zeros of a Factorial

#87 · Math · Intermediate · 70 XP · about 10 min

The factorial of n, written n!, is 1 × 2 × 3 × ... × n. Factorials grow very fast: 10! is 3,628,800, which ends in two zeros.

Write trailing_zeros(n) that returns how many zeros n! ends with. By definition 0! is 1, which ends in none.

Examples

trailing_zeros(5)     # returns 1    (5! = 120)
trailing_zeros(10)    # returns 2    (10! = 3628800)
trailing_zeros(25)    # returns 6

Constraints

  • 0 <= n <= 10^18
  • For large n the factorial has far more digits than any computer can hold, so count the zeros without building n!.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.