Close

The sound of sieving

A project log for A Tiny Sieve | Lehmer's Math Machine in ATtinys

Until the late 1990s, this device outperformed general purpose computers at certain number theory problems.

kbsriramkbsriram • 09/01/2026 at 17:41•0 Comments

How would you solve an equation like 

This isn't too hard of course, we can write

to get all possible x, y pairs. But what if we constrain x and y to be positive whole numbers?

Equations like this are Diophantine Equations, and are known to be hard to solve in the general case - e.g. with arbitrary powers and terms. If it has only squared terms like this (quadratic Diophantine equations) they are still known to be NP-complete, so we don't have any good way to solve them quickly either.

Let's try to brute-force our example anyway.

What we're looking for, are numbers of the type:

which are themselves square numbers, since y needs to be the square root.

Let's check for x = 1 - we get 2*1^2 + 4001 = 4003 - it's not a square number

Let's try a few more

x0123456789
2x^2 + 40014001400340094019403340514073409941294163

You might instinctively notice that some of these numbers - e.g. - 4033 don't look like it could be a square.

In fact, any perfect square ends in 0, 1, 4, 5, 6 or 9. The last digit (in base 10) is just arithmetic modulo 10, and so we can discard about half our numbers without going through the bother of actually calculating the square root.

We can do the same trick with a different base - let's take everything modulo 3, and see where that gets us

x mod 3012
x^2 mod 3011
2x^2 + 4001 mod 3211
could be square?noyesyes

The second row tells us only 0 and 1 can be perfect squares in mod 3 arithmetic.

The third row tells us that 2x^2 + 4001 = 2 mod 3 when x is 0 mod 3, and the second row tells us that 2 can never be a perfect square mod 3.

So this means any x that's 0 mod 3 can't be a solution either.

So we can skip about a third of our brute-force check already.

Let's do this trick for a couple more moduli

x mod 501234
x^2 mod 501441
2x^2 + 4001 mod 513443
could be square?yesnoyesyesno

mod 5 lets us avoid checking about 40% of our numbers

x mod 70123456
x^2 mod 70142241
2x^2 + 4001 mod 74651156
could be square?yesnonoyesyesnono

mod 7 lets us skip checking around 60% of our numbers.

x mod 11012345678910
x^2 mod 1101495335941
2x^2 + 4001 mod 118105473374510
could be square?nnyynyynyyn

and mod 11 lets us skip about 50% of our numbers.

We could of course keep stacking these tests. If we use prime moduli, each new  modulus removes about half the candidates, so this gets very effective very fast. If we use k moduli, only about 1/2k candidates survive the test. (The initial few primes usually remove less than half, so in practice sieves don't bother with moduli smaller than 9.)

The sieving mathematicians

Imagine you have four people in a room - one per modulus. Each of them has just the last row of yes/no values for their table. So we have one person holding the mod 3 row, another holding the mod 5 row, and so on.

A fifth person - a scorekeeper - holds a counter set at 0, and then shouts "vote".

Everyone looks at their first cell - if it says "no" - they shout "no". If it says "yes", they keep silent. Then, they move their finger to the next cell and wait for the scorekeeper. If they get to the end of their row, they just start over.

Most of the time, one or more people would have shouted no, and the scorekeeper just clicks their counter and shouts "vote" again.

Every once in a while, nobody in the room would have veto'ed that number. The scorekeeper looks at their counter, and checks if it could be a solution. If not, they keep moving on.

Let's see how this plays out in our example for the first 10 steps. Notice how the mod 3 and mod 5 rows just repeat every 3 times and 5 times respectively. Each row is just a pattern that repeats with a period equal to its modulus.

scorekeeper012345678910
the mod 3 personN----N----N----N--
the mod 5 person--N----N--N----N--
the mod 7 person--NN----NN--NN--
the mod 11 personNN----N----N----N

At least one person vetos every number through 10, and the scorekeeper just keeps clicking their counter. In fact, the first silence the scorekeeper hears is at 17, so they avoided checking all the previous numbers, and see if 17 could be a solution. 2*17*17 + 4001 = 4579 which isn't a perfect square, so they keep going. The next silence is at 25, then 28, and 35 - none of which are solutions. The next silence is at 38. 2 * 38 * 38 + 4001 = 6889 which is 83*83, so we have our first solution after checking only about 13% of all the numbers.

At four moduli, we were expecting to check only about 1/2^4 ~ 6% - but the initial small primes - 3 and 5 - only remove about a third, so we might have done better with slightly larger moduli.

The sieving ATtinys

Here is that room of people, built from three ATtiny412s.

Each ATtiny is programmed to hold the sequence for mod 3, mod 5 and mod 7. They listen on a shared line connected to a push-button switch. Anytime it goes low, they check the array and briefly pulse a different "veto" pin low.By default, the veto pins are pulled high across the tinys, and in high-Z mode (as inputs.) Pulsing briefly turns the pin to an output driving low, pulling it to ground. I can also connect all the veto pins together to get one line that rejects if any of the devices rejects.

I bread-boarded it and hooked up a buzzer to hear the rejected votes, and the repeating sequences from each ATtiny. Jumpers isolate each ATtiny for debugging. Hooking the buzzer to the shared line also shows how adding more moduli rejects more numbers audibly.

And here's a video so you can hear the rejections.

Discussions