Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code.

Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code.
n = 1000
c = 0
p = 2
while p <= n:
    c += n // p
    p *= 2
print(c)
What is printed?
  1. A. 500
  2. B. 249
  3. C. 994
  4. D. 9

Correct answer: C. 994

Legendre: 500 + 250 + 125 + 62 + 31 + 15 + 7 + 3 + 1 = 994 (also 1000 − popcount(1000)). Counting only even numbers gives 500, 249 is the count of factors of 5 (decimal zeros), and 9 counts powers of 2 up to 1000.

More Coding Interview Problems questions

Exams that ask this topic