Collatz Steps
Start with n. If the current number is even halve it, otherwise multiply it by 3 and add 1. Return how many steps it takes to reach 1 (0 if n is already 1).
Example 1
n 6
returns 8
6, 3, 10, 5, 16, 8, 4, 2, 1: eight steps.
Constraints
1 <= n <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Math problems
- Count DivisorsBeginner
- Decimal to Binary StringBeginner
- Digit Sum in Base KBeginner
- Digital RootBeginner
- Excel Sheet Column NumberBeginner
- Handshake CountBeginner
- Harshad NumberBeginner
- Hex String to DecimalBeginner