Living Dangerously With Math by Running the Collatz Conjecture on the ZX81
Running random numbers against the 3x+1 math problem can be fun.
As plans go, I actually have a few programs teed up to share. Many are smaller programs that I could work on here and there. Occasionally, I run across an idea that upends those plans and I end up going another direction. So, here we are in September with a math program for the Collatz Conjecture.
# Really? Math?
The most dangerous problem in math? I was watching YouTube shorts and ran across this video from @veritasium who talked about a the 3x+1 or Collatz conjecture. A deceptively simple math problem that has remained unproven for years.
The dangerous part more about how 3x+1 can consume a mathematician’s career, but is quite interesting. I found the Collatz conjecture's simplicity as an opportunity to write my own version. What better way to do that than on BASIC using a home computer from the earlier days of the home computer market? What could go wrong?
# What’s a conjecture?
Before getting to the program itself, let's talk a bit about what the Collatz conjecture is. The problem starts with a positive integer. If the number is odd, you multiple it by 3 and add 1, thus 3x+1. If the number is even, it is divided by 2. Because the number rises and falls quickly, Collatz is often referred to as a hailstone problem. I like the reference.
Now, what invariably happens is that the numbers get down to 4, 2 and 1 then repeat. The conjecture is that any this will happen for all positive integers. A conjecture is a mathematical statement that is proposed without proof. Observed evidence may suggest it as true—such as the case with 3x+1, but it isn’t proven so. Thus far, it has been tested on extremely large numbers. Yet, until a proof is found, it is just conjecture.
# Pick a number.
Not to let that stop me, I got to work on writing my own version of the math. Intrigued by the short picking a number, my first version of the program did the same. You entered in a number, with appropriate validations, and then the problem is ran. In this version, it just printed the hailstones until they dropped to the ground and equalled one.
Although this program worked well, it wasn’t very exciting. I mean, it works fine but the interactive nature is short lived. Due to that, I shifted to picking a random number that the program then runs through the problem.
Collatz Conjecture, Starting ZX81 Screenshot, 2024 by Steven Reid
As easy as that was, I decided I needed to clean up the display a bit. I mean, printing numbers needs some flair. Diving back into the code, I devised a little routine to center the numbers in the sequence. When complete, the routine prints how many iterations it took to reach one. After a short pause, it continues once more.
# Not quite a dangerous program.
Running the program for awhile, I reached the limits of the ZX81’s floating point math. Although I was only using integers, the ZX81 stores the numbers in a 5 byte floating point binary representation. This limits the precision on larger numbers causing those numbers to get padded with zeros.
Although the program still runs, it isn’t an accurate representation. To fix that, I should use a custom number format. It would be a fun experiment but the conjecture worked well enough. I dismissed that idea for a later implementation.
Collatz Conjecture, Ending ZX81 Screenshot, 2024 by Steven Reid
Some other modifications came to mind after I completed the program. One idea was to run the sequence of numbers. Perhaps not as interesting as random numbers, I was thinking about storing results and plotting them as it ran. It would make the program more interesting, but the screen real-estate is minimal on the ZX81.
Although great ideas, I’ll stick to this version. Math can be fun, and I quite enjoyed making something of it. I wonder if there are other conjectures I could play with?