3X+1 and Color BASIC

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.

Raspberry Pi 400

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!

11 thoughts on “3X+1 and Color BASIC

  1. William Astle

    First: 32767 is 15 bits, not 7 bits. :)

    Second: You might try something like this for detecting evenness:

    At the start: H=0.5

    Then the test: T=X*H:IF T<>INT(T) THEN do the odd thing

    The multiplication should be faster than division. Setting H avoids the conversion of 0.5 to floating point during every test which should be substantially faster. Also, using T avoids doing the multiplication twice during the test which should be an even bigger win.

    Reply
    1. Allen Huffman Post author

      Math is hard! I was only off by a bit… or a few bits. :) Using INT was one I was thinking of. I’ll see if I can do some revisions tonight. Thanks!

      Reply
  2. RogelioP

    Indeed the INT(X/2)*2=X even/odd detect will improve the range of the numbers the CoCo can handle – tried it as high with 6,171 – requires about 261 steps to finalize the series. Will see about trying it in BASIC09

    Reply
    1. Allen Huffman Post author

      I tried a negative to see what happened. I think the video I linked to mentioned something about negatives, but I don’t recall. I’ll make an update. Thank you!

      Reply
  3. Pingback: 3X+1 in C# | Sub-Etha Software

  4. Pingback: Odd or Even in Color BASIC? | Sub-Etha Software

  5. Pingback: 3X+1 in C# | Sub-Etha Software

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.