ProgrammingHardProduct company OA

What is printed?

What is printed?
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main(void) {
    int l = 1;
    for (int i = 1; i <= 10; i++) l = l / gcd(l, i) * i;
    printf("%d", l);
}
  1. A. 2520
  2. B. 5040
  3. C. 3628800
  4. D. 1260

Correct answer: A. 2520

lcm(l, i) = l / gcd(l, i) · i, and folding it over 1 to 10 gives the smallest number divisible by all of them: 2^3 · 3^2 · 5 · 7 = 2520. Dividing before multiplying keeps the intermediate small. 3628800 is 10!.

More C Programming questions

Exams that ask this topic