As you wrote this, it does not end, so it, for example, ends with alternating between 1 and 4, 2, etc. (all recursive descriptions should ultimately be somewhere below, and in your case there is no case to do this with n=1 ).
It works:
ClearAll[collatz]; collatz[1] = 1; collatz[n_ /; EvenQ[n]] := collatz[n/2] collatz[n_ /; OddQ[n]] := collatz[3 n + 1]
although it does not list intermediate results. A convenient way to get them is
ClearAll[collatz]; collatz[1] = 1; collatz[n_ /; EvenQ[n]] := (Sow[n]; collatz[n/2]) collatz[n_ /; OddQ[n]] := (Sow[n]; collatz[3 n + 1]) runcoll[n_] := Last@Last @Reap[collatz[n]] runcoll[115] (* -> {115, 346, 173, 520, 260, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1} *)
or
colSeq[x_] := NestWhileList[ Which[ EvenQ[
for example
colSeq[115] (* -> {115, 346, 173, 520, 260, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1} *)
By the way, the fastest approach I could come up with (I think I need this for some kind of Euler project problem) was something like
Clear@collatz ; collatz[1] := {1} collatz[n_] := collatz[n] = If[ EvenQ[n] && n > 0, {n}~Join~collatz[n/2], {n}~Join~collatz[3*n + 1]]
compare:
colSeq /@ Range[20000]; // Timing (* -> {6.87047, Null} *)
a
Block[{$RecursionLimit = \[Infinity]}, collatz /@ Range[20000];]
(we need to increase the recursion limit so that it works correctly).