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);
}- A. 2520
- B. 5040
- C. 3628800
- 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
- What is printed? c int isqrt(int n) { int lo = 0, hi = n, r = 0; while (lo <= hi) { int m = lo + (hi - lo) / 2; if ((long long)m m <= n) {…
- What is printed? c struct N { struct N nx; } n[5]; for (int i = 0; i < 4; i++) n[i].nx = &n[i + 1]; n[4].nx = &n[2]; struct N s = n, f =…
- On a two's-complement machine with C99 division, what is printed? c printf("%d %d", -13 % 8, -13 & 7);
- Assume IEEE-754 single-precision float and 32-bit unsigned int. What is printed? c union { float f; unsigned u; } v = {1.0f}…
- What is printed? c #line 100 int a = LINE; int b = LINE; printf("%d %d", a, b);
- What is printed? c #define CAT(a, b) a##b printf("%d", CAT(1, 2) + CAT(0x, 1F));