DEV Community

Cover image for Every prime ends in 1, 3, 7 or 9. Put them on a compass and you get a clock
Sayed Lotfy
Sayed Lotfy

Posted on AI-assisted

Every prime ends in 1, 3, 7 or 9. Put them on a compass and you get a clock

On my first day learning Java I met a sentence I expected to be technical: everything is an object.

An object is created, it does its work, it ends. The instructor moved on to constructors. I didn't. I kept turning the sentence over on the drive home. If everything is an object — if that's really how we're meant to see a program — what happens if you look at numbers that way? Not as values, but as things with a lifecycle: a beginning, a life, an end.

That question followed me for five years. This is what came out of it, and — because this is a room full of people who test things — what didn't survive testing.

The object model of a prime

A composite number has a social life. 12 is born at 1, and along the way it's touched by 2, 3, 4 and 6 before it gets to itself. Its path crosses everyone's.

A prime is different. It's born at 1 and ends at itself, and nothing touches it in between. A short, sealed lifecycle. No dependencies. If you were modelling numbers as objects, primes would be the immutable value types.

That was the whole starting point. It doesn't prove anything. But it changed what I looked at next.

The four constructors

Every prime past 5 ends in 1, 3, 7 or 9. That's not a pattern, it's arithmetic: anything ending in an even digit divides by 2, anything ending in 5 divides by 5. Four possible last digits, and every prime from 7 onward gets exactly one of them.

So — thinking in objects — there are four kinds of prime, and each kind has its own factory. I put them in four parallel lanes:

lane 1:  11, 31, 41, 61, 71, 101, ...
lane 3:  13, 23, 43, 53, 73,  83, ...
lane 7:  17, 37, 47, 67, 97, 107, ...
lane 9:  19, 29, 59, 79, 89, 109, ...
Enter fullscreen mode Exit fullscreen mode

The lanes don't take turns. They advance independently — each one just takes the next prime with its ending, whenever one arrives. Row n of the table is the n-th object each factory produced.

Then I put the four endings on a compass — 1 north, 3 east, 7 south, 9 west — and let a hand jump to whichever lane fired most recently. I built it in Angular so I could watch it run. That's the app at primeticktock.com.

And watching it, the list of primes stopped looking like a list. It started looking like a clock. The hand turns. Mostly clockwise.

Measuring the clock

"Mostly clockwise" is the kind of claim that gets a developer fired, so I measured it. Over every prime below 30,000,000 — 1,857,854 consecutive steps:

Move Share If random
Clockwise 31.0% 25%
Across 28.6% 25%
Counter-clockwise 23.7% 25%
Stay 16.8% 25%

The clock leans. And the reason turns out to be completely mundane once you see it. With the endings in the order 1 → 3 → 7 → 9, a small step forward on the number line is a small step clockwise on the dial. Twin primes (gap 2) are always one clockwise quarter-turn. A gap of 8 is always counter-clockwise. Small gaps are more common than large ones. So the hand leans clockwise because primes are usually close together.

That's a nice thing to watch. It is not a discovery. Chebyshev noticed the uneven distribution of prime endings in 1853, and Lemke Oliver and Soundararajan at Stanford mapped how consecutive primes avoid repeating an ending in 2016. I'm standing on their work and saying "look, it turns."

What failed in testing

The table looks like it should predict things. Three cells in a row filled, one empty — surely the empty cell tells you the next prime's ending?

I tested it. It scores 25.4% against a coin-flip's 25%. You can break it by hand: after placing every prime up to 47, the empty cell says the next prime ends in 9. The next prime is 53.

I'd believed that rule for about two years. Writing the test that killed it was the most useful afternoon of the project.

What survived

One thing. If you order the four lanes by recency — which one fired most recently — that ordering carries a little more information about the next prime than the standard method mathematicians use. About 2.6 percentage points more, against a proper null model. Small, real, and I can't explain it. It might be a known consequence of something I haven't read.

So I wrote it up so anyone can check the arithmetic, put it on Zenodo with a DOI, and emailed a mathematician who lives ten minutes from me with a note that said, more or less, please tell me where this goes wrong.

Why bother, if it proves nothing

Because of who it's for. I made the app, a book and twelve short films for the person who was told at school they weren't a maths person. I was that person. The clock asks nothing of you but counting. No formulas on screen. It lets you watch primes behave, feel the tug of a pattern that's really there, and then learn — from the last film — why the tug is not a law.

That's the whole idea. Everything is an object. A prime is an object with a sealed lifecycle. Put four kinds on a compass and they turn into a clock. The clock is honest about what it can't do, and I've tried to be too.


The clock (Angular, runs in any browser): primeticktock.com
The paper, with all the numbers: doi.org/10.5281/zenodo.22758494
Twelve short films, six in English and six in Arabic, free: YouTube playlist

The idea is open. It may be right, it may be wrong, and I'd rather hear where it's wrong from you than not hear it.

Top comments (0)