Collatz Steps
easyMath & Number Theory+10 XPTime limit 2s
Start with a positive number n and repeat this rule until you reach 1:
- if
nis even, divide it by 2; - if
nis odd, replace it with3n + 1.
Starting from 6: 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1, which takes 8 steps, and the largest value seen is 16.
For each starting number, print the number of steps and the largest value reached (including the starting number).
Input:
- line 1:
q - next
qlines: one integerneach
Output: q lines, each steps peak.
Constraints: 1 ≤ q ≤ 10⁴, 1 ≤ n ≤ 10⁶. The values can grow past 2³¹ — use 64-bit integers.
Think about it: just simulate the rule with a loop, counting steps and tracking the maximum. Starting at 1 takes 0 steps.
Sample input 1
2 6 1
Sample output 1
8 16 0 1
Sample input 2
1 27
Sample output 2
111 9232
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).