Data Structures & AlgorithmsModerateTCS NQT · number theory

Problem: How many trailing zeros does 100! have? Run the code that counts factors of 5.

Problem: How many trailing zeros does 100! have? Run the code that counts factors of 5.
n = 100
c = 0
p = 5
while p <= n:
    c += n // p
    p *= 5
print(c)
What is printed?
  1. A. 20
  2. B. 24
  3. C. 25
  4. D. 100

Correct answer: B. 24

Each trailing zero needs a factor 10 = 2 * 5, and 2s are always more plentiful, so counting 5s is enough. 100 // 5 + 100 // 25 = 20 + 4 = 24.

More Coding Interview Problems questions

Exams that ask this topic