Data Structures & AlgorithmsHardICPC regional · simulation
Problem: Starting from 27, the Collatz map is n -> n/2 if n is even and n -> 3n+1 if n is odd. How many steps does it take to first reach 1? Run the code.
Problem: Starting from 27, the Collatz map is n -> n/2 if n is even and n -> 3n+1 if n is odd. How many steps does it take to first reach 1? Run the code.
n = 27
steps = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
steps += 1
print(steps)What is printed?- A. 112
- B. 110
- C. 111
- D. 96
Correct answer: C. 111
The trajectory of 27 is famously long, peaking at 9232 before reaching 1 after 111 steps. Off-by-one loops that stop at 2 give 110, which is the trap.
More Coding Interview Problems questions
- Problem: Find the smallest positive integer n such that n! has at least 6 trailing zeros. Run the code. python def zeros(n): c = 0 p = 5…
- Problem: Find the length of the longest strictly increasing subsequence of [10, 9, 2, 5, 3, 7, 101, 18] with the O(n log n) tails method…
- Problem: Coins of values 2, 5, 10 and 1 are available with unlimited copies. What is the minimum number of coins summing to 27? Run the…
- Problem: How many ways are there to make change for 100 using coins of 1, 5, 10, 25 and 50 (order does not matter)? Run the code. python…
- Problem: Find the longest palindromic substring length of 'forgeeksskeegfor' by expanding around each centre. python s =…
- Problem: What does the following print? It counts distinct values in each window of size 3. python from collections import Counter a = [1…