><>, 27 26 23 bytes
\ln;
\::2%:@5*1+2,*+:2=?
Like the other ><> answers, this builds the sequence on the stack. Once the sequence reaches 2, the size of the stack is the number of steps taken.
Thanks to @Hohmannfan, saved 3 bytes by a very clever method of computing the next value directly. The formula used to calculate the next value in the sequence is:
$$f(n)=n\cdot\frac{5(n\bmod2)+1}{2}+(n\bmod2)$$
The fraction maps even numbers to 0.5, and odd numbers to 3. Multiplying by n
and adding n%2
completes the calculation - no need to choose the next value at all!
Edit 2: Here's the pre-@Hohmannfan version:
\ln;
\:::3*1+@2,@2%?$~:2=?
The trick here is that both 3n+1
and n/2
are computed at each step in the sequence, and the one to be dropped from the sequence is chosen afterwards. This means that the code doesn't need to branch until 1 is reached, and the calculation of the sequence can live on one line of code.
Edit: Golfed off another character after realising that the only positive integer that can lead to 1 is 2. As the output of the program doesn't matter for input < 2, the sequence generation can end when 2 is reached, leaving the stack size being the exact number of steps required.
Previouser version:
\~ln;
\:::3*1+@2,@2%?$~:1=?