ProgrammingHardTCS NQT · Accenture

Smallest n such that n! has at least 5 trailing zeros is searched for by the procedure below, where z(n) is the number of factors 5 in n! (sum of n div 5^k). What is printed as n?

Smallest n such that n! has at least 5 trailing zeros is searched for by the procedure below, where z(n) is the number of factors 5 in n! (sum of n div 5^k). What is printed as n?
n = 1
while z(n) < 5
    n = n + 1
end while
Print n
  1. A. 8
  2. B. 20
  3. C. 9
  4. D. 25

Correct answer: D. 25

n = 25 is the first value with five factors of 5 (25 contributes two). Using factors of 2 gives 8, using only the first power gives 20, and confusing trailing zeros with the size of the factorial stops at 9.

More Pseudocode and Code Tracing questions

Exams that ask this topic