Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the largest k such that 2^k divides 12!? Run the code.

Problem: What is the largest k such that 2^k divides 12!? Run the code.
n = 12
c = 0
p = 2
while p <= n:
    c += n // p
    p *= 2
print(c)
What is printed?
  1. A. 6
  2. B. 9
  3. C. 2
  4. D. 10

Correct answer: D. 10

Legendre: 12//2 + 12//4 + 12//8 = 6 + 3 + 1 = 10. Counting only the even numbers gives 6, stopping before 8 gives 9, and 2 counts the decimal trailing zeros of 12!.

More Coding Interview Problems questions

Exams that ask this topic