Analytica Data Science SolutionsContact
ESC

to move to open

← All insights

Insight

The Collatz conjecture: two rules, no exit

Halve it if it's even, triple-and-add-one if it's odd. Every number ever tried falls to 1, nobody knows why, and the picture is worth the trip.

December 2022MathematicsRNumber theory

Written some years ago. The ideas hold, but check package versions and API details against current documentation before relying on the code.

Take any positive whole number. If it’s even, halve it. If it’s odd, multiply by three and add one. Repeat.

Start with 5 and you get: 5 → 16 → 8 → 4 → 2 → 1. Start with 27 and you’re in for a ride — 111 steps, climbing as high as 9,232 before collapsing. But collapse it does, and that’s the conjecture Lothar Collatz posed in 1937: no matter where you start, you always reach 1.

Always? Every number ever tested reaches 1, and the testing has gone past 2⁶⁸. No proof exists. Paul Erdős famously said mathematics isn’t ready for such problems, and offered $500 for a solution — pocket change against the million on Riemann, yet the problem has eaten just as many careers.

Why so hard, when the rule is so small?

Because the sequence has no respect for size or pattern. Whether you rise or fall next depends only on parity, and parity after tripling-plus-one is chaotic in practice. Neighbouring starting points produce wildly different journeys, which you can see directly:

Total stopping time for each starting value: a jagged, self-similar skyline with no obvious order.

That plot is the length of the sequence for each starting number. 26 takes ten steps; 27 takes 111. There’s structure in there: bands, echoes, a strange self-similarity. But nothing anyone has turned into a proof. The serious result we do have is Terence Tao’s from 2019: almost all starting values eventually get almost as small as you like. “Almost all” is doing heavy lifting there. It’s the strongest statement anyone has managed, and it still doesn’t rule out one immortal orbit somewhere, spinning forever.

Run it yourself

The whole thing fits in a dozen lines of R:

collatz <- function(n) {
  steps <- n
  while (n != 1) {
    n <- if (n %% 2 == 0) n / 2 else 3 * n + 1
    steps <- c(steps, n)
  }
  steps
}

collatz(27)
# 27 82 41 124 62 31 94 47 142 71 214 107 ... 4 2 1

# Trajectory lengths for the first 10,000 starting values
lengths <- sapply(1:10000, function(n) length(collatz(n)))
plot(lengths, type = "h", xlab = "Starting value n",
     ylab = "Length of Collatz sequence")

Change the rule slightly, to 3n − 1 instead of 3n + 1, and you get cycles that never reach 1. So the conjecture is delicately balanced on its exact form, which somehow makes it worse.

Why do I keep a soft spot for this one, alongside Goldbach? Because it’s the purest example I know of a lesson our whole field runs on: a system’s rules being simple tells you nothing about its behaviour being simple. Four lines of code, eighty years of failure. I show this to people who think complexity requires complication, and I watch it land every time.

Discuss a project

Tell us what decision, workflow, or data problem you're working on. We'll tell you what the data you already have can support, and what a first engagement looks like.

Start a conversation