ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int calls = 0;
int g(int a, int b) {
calls++;
return b ? g(b, a % b) : a;
}
int main(void) {
int r = g(2025, 1500);
printf("%d %d", r, calls);
}- A. 75 4
- B. 25 5
- C. 75 5
- D. 75 6
Correct answer: C. 75 5
Euclid's recursion gives gcd(2025, 1500) = 75 through the calls (2025, 1500), (1500, 525), (525, 450), (450, 75) and (75, 0), which is five calls.
More C Programming questions
- How many ways are there to climb 20 steps taking 1 or 2 at a time? Compute with this exponential recursion. c #include <stdio.h> long long…
- What is printed? c #include <stdio.h> int calls = 0; int bs(int a, int lo, int hi, int k) { calls++; if (lo > hi) return -1; int m = lo +…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0; int c = 0; for (unsigned i = 1; i <= 16; i++) { x = i; while (x) {…
- What is printed? c #include <stdio.h> unsigned alt(unsigned x) { unsigned y = x ^ (x >> 1); return (y & (y + 1)) == 0; } int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xDEADu; printf("%u", (x >> 3) & 0xFu); }
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 0x12345678u, b = 0x12345600u, x = a ^ b; int c = 0; while (x) { c += x…