ProgrammingHardTCS NQT · Accenture

The least common multiple of the integers 1 through 10 is computed by folding lcm(a, b) = a b div gcd(a, b) from L = 1. The procedure below prints the result. What is printed?

The least common multiple of the integers 1 through 10 is computed by folding lcm(a, b) = a * b div gcd(a, b) from L = 1. The procedure below prints the result. What is printed?
L = 1
for i = 1 to 10
    L = L * i div gcd(L, i)
end for
Print L
  1. A. 2520
  2. B. 3628800
  3. C. 840
  4. D. 55

Correct answer: A. 2520

Dividing by the gcd at each step gives 2520, which is the lcm of 1 to 10. The product 10! = 3628800 ignores the shared factors, stopping at 8 gives 840, and the sum 55 is an unrelated quantity.

More Pseudocode and Code Tracing questions

Exams that ask this topic