Data Structures & AlgorithmsHardCodeforces Div2 B · factorial factors

Problem: Find the smallest positive integer n such that n! has at least 6 trailing zeros. Run the code.

Problem: Find the smallest positive integer n such that n! has at least 6 trailing zeros. Run the code.
def zeros(n):
    c = 0
    p = 5
    while p <= n:
        c += n // p
        p *= 5
    return c
n = 1
while zeros(n) < 6:
    n += 1
print(n)
What is printed?
  1. A. 20
  2. B. 30
  3. C. 24
  4. D. 25

Correct answer: D. 25

Trailing zeros of n! depend only on factors of 5. 20! has 4 zeros and 25! has 25 // 5 + 25 // 25 = 6 zeros, so 25 is the first n that works.

More Coding Interview Problems questions

Exams that ask this topic