Here's my contribution...hopefully it is interesting and enlightening. I tried to do a language different than the ones that had already been tackled:
PROLOG
next(Num,Nextnum) :- Num>0, X is mod(Num,2), X is 0, Nextnum is Num//2, write(Num), nl.
next(Num,Nextnum) :- Num>0, X is mod(Num,2), X is 1, Nextnum is 3*Num+1, write(Num), nl.
prog(Num,Iter) :- Num>1, next(Num,Nextnum), prog(Nextnum,I), Iter is I+1.
prog(1,1) :- write(1).
The solution is wonderfully succinct. There are four rules. The first tests whether the first number is even and returns the next number. The second tests whether the first number is odd and returns the next number. If the number is less than 0, both rules will automatically return false. The next rule is given a starting number and iterates until the fourth rule, the base case, is true.
So if I were to type "prog(10,X).", the program would return:
10
5
16
8
4
2
1
X = 7 ;
no (this signifies that there aren't multiple answers, which is good )
So there you go. I really like Prolog, it requires you to think a little differently.
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
I thought I'd see how this thread was doing, and I also thought I'd add another language or two to the list. Here's a solution in Scheme, compiled and tested:
To someone who isn't familiar, and especially when seen in its non-indented state, all those parentheses must seem pretty scary. The periods at the beginning of every line mean nothing, I just put them there to add much-needed indentation. I threw in a helper function so I could keep a running count of the number of steps to reach the goal...if you consider that count unnecessary, then the code is a little shorter and more straightforward. The code can be run after compilation by typing (prob 10) or (prob 143) or whatever.
Now, since I wrote the Scheme code, and since Scheme is a dialect of Lisp, I might as well put the Lisp code here too. It's very similar, but unfortunately, I don't have a Lisp compiler on this computer, so it remains untested. As I am new to Lisp, there very well could be a problem:
(defun prob
....(labels ((helper (n counter)
................(format t "~A~%" n)
................(cond
..................((= n 1)
...................(format t "Reached goal in ~A steps~%" counter))
..................((even n)
...................(helper (/ n 2) (+ 1 counter)))
..................(t
...................(helper (+ (* 3 n) 1) (+ 1 counter))))))
......(helper n 1)))
With all the built-in functions that Common Lisp has, I wouldn't be surprised if there's one out there that could do the recursive counting for me, but I don't know what it is and don't want to take the time to look.