Collatz Steps
The Collatz conjecture defines a process for any positive integer. Given an integer $n$, perform the following operations repeatedly until $n$ becomes $1$:
- If $n$ is even, replace $n$ with $n / 2$.
- If $n$ is odd, replace $n$ with $3n + 1$.
It is conjectured that this process will always reach $1$, no matter the starting value of $n$. Your task is to determine how many steps it takes for a given $n$ to reach $1$.
For example, if $n = 3$, the sequence is: $3 \rightarrow 10 \rightarrow 5 \rightarrow 16 \rightarrow 8 \rightarrow 4 \rightarrow 2 \rightarrow 1$. This takes $7$ steps.
Example 1
Input
n = 6Output
8Explanation
The sequence for n = 6 proceeds as follows: 1. 6 is even → 3 2. 3 is odd → 10 3. 10 is even → 5 4. 5 is odd → 16 5. 16 is even → 8 6. 8 is even → 4 7. 4 is even → 2 8. 2 is even → 1 It takes 8 steps to reach 1.
Example 2
Input
n = 1Output
0Explanation
The number is already 1, so 0 steps are required.
Example 3
Input
n = 3Output
7
Constraints
1 <= n <= 10⁶
Collatz Steps Editorial
Overview
The problem asks us to simulate the Collatz conjecture (also known as the $3n + 1$ problem). We are given a starting positive integer $n$ and must follow two specific rules based on whether the current number is even or odd. We repeat these rules until the number reaches $1$, counting every operation as a single "step."
The core of the challenge is simply implementing the simulation logic while ensuring we handle the base case where $n = 1$ correctly from the start.
Brute Force
The most straightforward way to solve this is using a while loop. We initialize a counter at $0$ and continue the loop as long as $n$ is not equal to $1$. Inside the loop, we check the parity of $n$ using the modulo operator (% 2) and update $n$ accordingly.
def collatz_steps(n: int) -> int:
steps = 0
# Continue until we reach the target value of 1
while n != 1:
if n % 2 == 0:
# If even, divide by 2
n //= 2
else:
# If odd, multiply by 3 and add 1
n = 3 * n + 1
steps += 1
return steps
- Time complexity: O(log n) to O(n) empirically — While the Collatz conjecture is unproven, for $n \le 10^6$, the sequence length is known to be relatively small (the maximum steps for $n < 10^6$ is 524).
- Space complexity: O(1) — We only store two integer variables,
stepsandn.
Optimal / Best Approach
For a single query where $n \le 10^6$, the iterative simulation is already highly efficient. However, in an interview context, you might be asked how to handle multiple queries (e.g., finding steps for many different values of $n$).
In that case, we can use Memoization. By storing the results of previously calculated numbers in a hash map, we can short-circuit the simulation when we land on a number we have already processed. Note that while $n$ starts at $10^6$, the sequence can grow much larger (the "peak" value), so a dictionary is better than a fixed-size array.
memo = {1: 0}
def collatz_steps(n: int) -> int:
# If we already know the steps for this n, return it immediately
if n in memo:
return memo[n]
# Calculate the next value in the sequence
if n % 2 == 0:
next_n = n // 2
else:
next_n = 3 * n + 1
# Recursive step: steps for n is 1 + steps for the next number
memo[n] = 1 + collatz_steps(next_n)
return memo[n]
- Time complexity: O(N) total for multiple queries — Each unique number in the sequence path across all queries is computed exactly once.
- Space complexity: O(N) — We store the step count for each unique number encountered in the
memodictionary.
Edge Cases
- n = 1: The loop condition
while n != 1handles this naturally. If $n$ starts as $1$, the loop body never executes, and we return $0$ steps, which is correct. - Large Peaks: Even for $n = 10^6$, the values in the sequence can exceed $10^6$ significantly before falling back to $1$. Python handles arbitrarily large integers automatically, so overflow is not a concern as it might be in C++ or Java (where
long longwould be required).
Why this works
The Collatz process is deterministic. Since we are guaranteed (conjectured and verified for all numbers of this magnitude) to reach $1$, the iterative approach will always terminate. The use of integer division // in Python is preferred to ensure $n$ remains an int type rather than becoming a float.
Fast
Comments on the solution
No comments on the solution yet. Start a thread about the editorial or your own approach.
No comments yet for Collatz Steps. Be the first to start the thread.
No submissions recorded for this problem yet. Use Submit in the editor — each judge run is stored here.
Output
Input
Expected
Your output
Run to see your output.