For the past three weeks, I have found myself out-of-town for work. This week, I decided to bring my Raspberry Pi 400 along so I could play with it in the hotel room.

I soon found myself toying around with the XRoar CoCo emulator, and I knew just what I wanted to program…
Last night, YouTube showed me a video about “the most dangerous problem in mathematics.”
The idea is you start with a number. If it is odd, you multiply it by 3 and add 1. If it is even, you divide it by 2. Repeat until you get to the pattern 4, 2, 1, 4, 2, 1, 4, 2, 1.
Math says that, so far, every number ends up at that pattern. No one has figured out a formula that leads to any number that does not end at 4, 2, 1.
With that in mind, I thought it would be fun to write the 3X+1 problem in CoCo Color BASIC. It looks like this:
0 REM 3X+1 10 PRINT:INPUT "STARTING NUMBER";X 20 PRINT X; 30 IF X=1 THEN 10 40 IF X AND 1 THEN X=X*3+1:GOTO 20 50 X=X/2:GOTO 20
I tried to avoid using any Extended Color BASIC features such as HEX numbers (&H1) to speed things up. I even skipped ELSE so it could run on a VIC-20 or other system without that command.
I use “X AND 1” to test for a number being odd. Any odd number has the first bit set. 1 (00000001) does, 2 does not (00000010), 3 does (00000011), and so on.
It does have one flaw… if any number is greater than 32767, it will crash with a ?FC ERROR. Apparently Color BASIC’s AND cannot handle any value greater than 7 bits (01111111 = 32767).
Do you know of a different way to test for even or odd values? I can think of two, one of which would be terribly inefficient and the other not as inefficient but much worse than using AND.
Give it a shot and see if you can find the largest sequence of numbers a CoCo can calculatae.
Or better yet, find a better way to do this in Color BASIC.
Enjoy!
