## The Collatz conjecture

One of the recent reads took me down the mathematics path.  This is always a bit of fun.  The story was:

6 deceptively simple maths problems that no one can solve

I remember the Collatz conjecture from years ago.

Basically, it says to take any number.  If the number is even, divide it by two and continue with the result.  If the number is odd, then triple it, add one, and continue with the result.  The conjecture says that, if you repeat it enough times, you’ll eventually get the number 1.

During dog walks yesterday, I found myself mentally doing the problem.  When I didn’t lose track, the sequence of numbers always did end up at 1.

When I got home, I thought; this is silly to do all this mental math.  I should write a program.

Here it is in Small Basic.  I’ve tried to make it readable and over-commented to explain the steps.  I think it should be readable enough.

And, of course, I ran it to make sure that it worked.  I played a lot of “kitten on the keyboard” to test it out and sure enough, the answer is always eventually 1.

For simplicity, here are 6 and 7.

Another observation include that, in addition to the answer being 1, any number I tried over 6 ended in the sequence 10 5 16 8 4 2 1.  Of course, I didn’t try every number.  However, I did modify the original program to loop the original number tested instead of just working with a single input.

After writing the program, I like the programming concepts that it includes.

• Asking the user for input
• Looping until an exit condition is met
• A little mathematics
• A little branching