Inspired by http://xkcd.com/710/ here is a code golf for it.
The Challenge
Given a positive integer greater than 0, print out the hailstone sequence for that number.
The Hailstone Sequence
See Wikipedia for more detail..
- If the number is even, divide it by two.
- If the number is odd, triple it and add one.
Repeat this with the number produced until it reaches 1. (if it continues after 1, it will go in an infinite loop of 1 -> 4 -> 2 -> 1...
)
Sometimes code is the best way to explain, so here is some from Wikipedia
function collatz(n)
show n
if n > 1
if n is odd
call collatz(3n + 1)
else
call collatz(n / 2)
This code works, but I am adding on an extra challenge. The program must not be vulnerable to stack overflows. So it must either use iteration or tail recursion.
Also, bonus points for if it can calculate big numbers and the language does not already have it implemented. (or if you reimplement big number support using fixed-length integers)
Test case
Number: 21
Results: 21 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1
Number: 3
Results: 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
Also, the code golf must include full user input and output.
C# : 659 chars with BigInteger support
Ungolfed
MS Excel, 35 chars
Taken straight from Wikipedia:
It only took copy/pasting the formula 111 times to get the result for a starting number of 1000. ;)
Scheme: 72
This uses recursion, but the calls are tail-recursive so I think they'll be optimized to iteration. In some quick testing, I haven't been able to find a number for which the stack overflows anyway. Just for example:
(c 9876543219999999999000011234567898888777766665555444433332222 7777777777777777777777777777777798797657657651234143375987342987 5398709812374982529830983743297432985230985739287023987532098579 058095873098753098370938753987)
...runs just fine. [that's all one number -- I've just broken it to fit on screen.]
Scala + Scalaz
And in action:
Scala 2.8
This also includes the trailing 1.
With the following implicit
this can be shortened to
Edit - 58 characters (including input and output, but not including initial number)
Could be reduced by 2 if you don't need newlines...
Golfscript : 20 chars
This is equivalent to
Perl
I decided to be a little anticompetitive, and show how you would normally code such problem in Perl.
There is also a 46 (total) char code-golf entry at the end.
These first three examples all start out with this header.
Simple recursive version
Simple iterative version
Optimized iterative version
Now I'm going to show how you would do that last example with a version of Perl prior to v5.10.0
Benchmark
First off the IO is always going to be the slow part. So if you actually benchmarked them as-is you should get about the same speed out of each one.
To test these then, I opened a file handle to
/dev/null
($null
), and edited everysay $n
to instead readsay {$null} $n
. This is to reduce the dependence on IO.After having run it 10 times, here is a representative sample output:
Finally, a real code-golf entry:
46 chars total
If you don't need to print the starting value, you could remove 5 more characters.
41 chars total
31 chars for the actual code portion, but the code won't work without the
-n
switch. So I include the entire example in my count.