ProgrammingVery hardGATE CS · Campus placements
Starting at n = 27, the loop while (n != 1) { n = (n % 2) ? 3 * n + 1 : n / 2; steps++; } runs with int n. What is steps at the end?
- A. 111
- B. 96
- C. 112
- D. 110
Correct answer: A. 111
The Collatz sequence from 27 takes 111 steps to reach 1, and it climbs as high as 9232 on the way. All values stay well inside int, so the loop is defined.
More C Programming questions
- Using square-and-multiply on int values with a reduction modulo 1000 after each multiplication, what is 3^13 mod 1000?
- How many 6-bit masks have exactly two 1 bits and have bit 0 cleared?
- The binary-reflected Gray code of i is g(i) = i ^ (i >> 1). What is g(4)?
- What is popcount(0xFFFF0000 ^ 0x0F0F0F0F)?
- int q = NULL; if (q != NULL && q == 3) printf("yes"); else printf("no"); What is printed?
- #define ADD(a,b) a+b. What does printf("%d", 2 ADD(1,2) 3) print?