Skills101
← All coding problems

Trailing Zeros of a Factorial

easyMath & Number Theory+10 XPTime limit 2s

n! (n factorial) is 1 × 2 × 3 × … × n, and 0! = 1. For example 10! = 3628800, which ends in 2 zeros.

For each n, print how many zeros n! ends with. The factorial itself is far too big to compute for large n — find the count without it.

Input:

  • line 1: q — how many queries
  • next q lines: one integer n each

Output: q lines — the number of trailing zeros of n!.

Constraints: 1 ≤ q ≤ 10⁴, 0 ≤ n ≤ 10¹⁸ (use 64-bit integers).

Think about it: every trailing zero comes from a factor 10 = 2 × 5, and there are always more 2s than 5s. So count the factors of 5: n/5 + n/25 + n/125 + … (integer division).

Sample input 1

3
10
5
4

Sample output 1

2
1
0

Sample input 2

2
25
0

Sample output 2

6
0

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.