collatz(1) :- write(1).
collatz(N) :- write(N), write(','), N mod 2 =:= 0, M is N/2, collatz(M), !.
collatz(N) :- write(N), write(','), M is N*3+1, collatz(M).