Skills101
← All coding problems

Collatz Steps

easyMath & Number Theory+10 XPTime limit 2s

Start with a positive number n and repeat this rule until you reach 1:

  • if n is even, divide it by 2;
  • if n is odd, replace it with 3n + 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 q lines: one integer n each

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).

Loading editor…

Log in to run and submit your code — it's free.