Collatz conjecture
Today at Hacker News»
Do you like Math? And have you heard about 3n+1, yet? I just figured it out today, when it was in the top 100 at Hacker News, so I stumbled over this youtube video about ‘The Simplest Math Problem No One Can Solve’.
There are different names for this row algorithm and you can probably look up more informations on this on your own. I was immediately interested in making some visual coding and ‘feel the numbers’ all by myself, the seemingly random peaks on the one hand and the always exact same ending on the other.
You might get the nobel prize in mathematics if you can prove, why this row ends for all numbers at 1. Or you find a number that gets caught in a loop and never reaches 1?
Visualization»
I’ve created these three visualizations/animations as Svelte components. I came up with the idea to show the steps as fragments of a circle to see if there’s some hidden pattern. ;)
Algorithm»
The rules for a Collatz row are so simple: Pick a number and if it is
- odd, multiply with 3 and add 1
- even, divide by 2
Repeat and you’ll always end at number 1.
In terms of Javascript code, it’s so simple as well.
const applyRules = (num) => (num & 1) ? num * 3 + 1 : num / 2;
CAUTION: Addictive»
But before you worry about it now yourself, be warned, some say this topic could quickly become addictive. There’s of course already a xkcd for the Collatz conjecture, too.