Simple Collatz Sequence
InterviewTime limit1sMemory limit512 MB
Simulate the modified Collatz rule (halve even k, increment odd k) from n until reaching 1 and count the steps taken.
- Level
Easy2 of 10
- Topics
- Simulation, Implementation, Math
- Solved
- No attempts yet
Problem
The Simple Collatz Sequence (SCS) starting at an integer is defined by the formula:
The sequence is then until the value first reaches 1.
For example, starting at 11, we have:
11 -> 12 -> 6 -> 3 -> 4 -> 2 -> 1
The sequence always ends at 1. (Fun Fact: The Hard Collatz Sequence sends odd to . It is unknown whether that sequence always ends at 1.)
Let be the number of steps in the SCS starting at . For example, . Write a program which computes for a given input .
Input
Input consists of a single line which contains a positive decimal integer, , which starts the sequence. will fit in a 32-bit unsigned integer.
Output
The output consists of a single line that contains the value of , the number of steps in the SCS starting at .