ProgrammingHardTCS NQT · Accenture
Starting from n = 27, the Collatz step is applied: if n is even, n becomes n/2, otherwise n becomes 3n + 1. The procedure below counts the steps until n becomes 1. What is printed?
Starting from n = 27, the Collatz step is applied: if n is even, n becomes n/2, otherwise n becomes 3n + 1. The procedure below counts the steps until n becomes 1. What is printed?
steps = 0
while n != 1
if n mod 2 == 0 then n = n div 2 else n = 3*n + 1
steps = steps + 1
end while
Print steps- A. 55
- B. 112
- C. 9232
- D. 111
Correct answer: D. 111
Starting at 27 the sequence takes 111 steps before reaching 1 and peaks at 9232. Printing half the step count gives 55, stopping the count one early gives 112, and printing the peak value 9232 answers a different question.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Four numbers 4, 6, 10 and 15 are combined pairwise: the lcm of a and b is computed as ab div gcd(a, b), and the lcm of three numbers is…
- Count the integers n with 1 <= n <= 100 that are divisible by 3 or by 5, but NOT by both. The procedure below does this with a single…
- For N = 100 the procedure below counts how many times 5 divides the factorials, that is, the number of trailing zeros of 100!. The count…
- The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed? rev = 0; m = N…
- An Armstrong number of three digits equals the sum of the cubes of its digits. The procedure below counts how many three-digit numbers…
- Compute C(10, 3) modulo 7 using Pascal's rule C(n, k) = C(n-1, k-1) + C(n-1, k), reducing every entry mod 7. What is printed? build the…