Skip to content
Subscribe
All writing
MathLeanAI agentsFormal proof

Why the Remainders Have to Repeat (OEIS A340880)

A sequence built from a long sum of powers. Keep only the remainders after dividing by any fixed number and they always fall into a loop. The proof is a counting argument, and the first version failed the real checker.

Here is a fact that feels like it needs deep math and turns out to need only counting. Take a sequence of numbers built by a fixed rule. Divide each one by some number N and keep only the remainder. If the rule is the right kind, the remainders must eventually run in a loop.

A 2021 conjecture by Peter Bala on the OEIS page A340880 said this about one particular sequence. My AI agent's proof is 638 characters. The first draft of it failed the real checker, which is the part worth telling.

The sequence

The exact formula is a mouthful: a sum where each piece is a power of one number times a long product of other powers. What matters is that it obeys a simple step-by-step rule. If you call the base number b, then

next value = (b^n - 1) × current value + b^(n(n+1)/2)

With b = 2 the sequence starts 1, 3, 17, 183, 3769, 149607. It grows very fast.

Now keep only the last digit of each number. The last digit is the remainder after dividing by 10.

The last digits of the sequence for n from 1 to 24, repeating in a loop of length 8

1, 3, 7, 3, 9, 7, 3, 7, and then the same eight again, and again. The conjecture says that some loop like this shows up for every base and every divisor. It may take a while to start. It always starts.

Why it has to loop

To compute the next remainder you need three things, each of them only as a remainder:

  • the current value
  • b^n
  • b^(n(n+1)/2)

And each of those three can be updated using only the three themselves. The current value updates by the rule above. b^n becomes b^(n+1) when you multiply by b. The third one updates by multiplying it by the new b^(n+1).

So picture a machine with three dials. Each dial shows a remainder, a number from 0 to N minus 1. One turn of the crank moves all three dials, and where they move depends only on where they are now.

Three dials with N positions each have N × N × N possible settings. That is a finite number. Turn the crank more times than there are settings and you must see some setting twice.

And once a setting repeats, everything after it repeats too, because the machine has no memory except the dials. Same setting, same next setting, forever.

That is the entire proof. It is the pigeonhole principle: more pigeons than holes means two pigeons share a hole.

What broke

One of the helper agents wrote this proof and it compiled in our scratch copy. Then the site's own checker, which the agent runs on my machine before anything is submitted, rejected it with errors like Unknown identifier on basic lemma names.

The cause was dull. Lean proofs sit on top of a huge shared library of known math called Mathlib. Our scratch copy of the problem loaded all of Mathlib. The official statement file for this problem loads only two small pieces of it. So the proof was calling tools that existed on our desk and not in the real room.

The fix was one line: load the full library at the top of the proof file. After that it passed, and the agent changed the submit script so every proof does that from then on.

I have made this exact mistake in ordinary software more than once. A test that passes in an environment slightly richer than production tells you nothing. The only check that counts is the one run by the same gate that will judge you. Running the site's own checker before every submission is the only reason this never went out as a failed one.

The numbers

The first accepted proof was 2,825 characters. Ours is 638, 77 percent shorter, accepted as pull request 60235.

Proof length: 2,825 characters for the first accepted proof, 638 for mine

Someone else proved it first. Ours is the shortest accepted proof on the problem page as of November 29, 2026.

This was one of 27 problems from the same evening. The first one, with the full story, is here.

Get the next build note.

What I'm building, what worked, and what broke, whenever there's something worth sharing.

Subscribe to my newsletter

Keep up with the work.

My notes on useful AI, the systems I build, and what I learn running a business.