ProgrammingVery hardCompetitive coding (CP)
How many steps does this Collatz loop take to reach 1 starting from 27?
How many steps does this Collatz loop take to reach 1 starting from 27?
#include <stdio.h>
int main(void) {
unsigned n = 27;
int steps = 0;
while (n != 1) {
n = (n % 2) ? 3 * n + 1 : n / 2;
steps++;
}
printf("%d", steps);
}- A. 110
- B. 112
- C. 111
- D. 96
Correct answer: C. 111
The loop applies the odd and even rules until n reaches 1, which takes 111 steps for 27. The peak value on the way is 9232, which does not change the step count.
More C Programming questions
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { return (const int )a - (const int )b; }…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { int a = -1; unsigned b = 2; printf("%u"…
- What is printed? c #include <stdio.h> int main(void) { unsigned k = 0; int len = -1; printf("%d", k > len); }
- How many integers in 0..15 are a power of two? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for (unsigned x…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {3, 1, 2}, swaps = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 2…
- What is printed? Assume uint32t is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint32t h = 5381; const char s =…