Prime Numbers Box project: learn cryptography
By Russell Barnes. Posted
Get a new prime number at the push of a button
Like all good geeks we love our prime numbers. They're one of the most interesting things to study in mathematics.
Maybe that's why we find this Prime Numbers in a Box project so endearing. It's pretty simple in practice, press a button; get a prime number.
"How many times have you needed the next prime number in a sequence and, like some animal, had to go to a printed table to look it up," says WhiskyTangoHotel. "Well, those days are over."
- Build information: Prime Numbers in a Box
The importance of prime numbers
These numbers are popular in encryption and cryptography. This is because the numbers, which are only divisible by themselves and one, can be multiplied together.
If you multiply two primes together, you get a public key. This number is used to lock data and can be shared freely. But to unlock the data, you need to know the two primes used to generate it.
While it's easy for a computer to generate the key from the two primes, it's very difficult for the computer to get the two numbers from the key.
There's a great video here by Simon Pampena that explains how prime numbers and cryptography work.
Learn about primes by building a box that generates them
While WhiskeyTangoHotels project doesn't create primes for cryptography. Instead, the build just celebrates the existence of these great numbers.
"The project has an entertaining audio effect if you are into numbers," says the maker. "Press a button and Primes in a Box gives an audible (relay click) signal for each non-prime as it waits to display the next found prime. If you enjoy mathematics, you may find this oddly relaxing."

"The Python source is pretty straightforward, explains the maker. "On button press, a pointer to a file containing the first few million primes is indexed and displayed on the LCD. A 5VDC relay clicks to represent the non-primes in between. The rig runs via USB power, and the last found prime is always saved. A handy 'shutdown' button is incorporated to allow the Raspberry Pi to be powered down gracefully if it needs to be moved."
You'll learn a few things about reading and writing to text files in this project. And you'll learn how to use an LCD and relay. More importantly, you'll never be short of primes again. And you might even be your first step towards becoming a cryptographer.
Russell runs Raspberry Pi Press, which includes The MagPi, Hello World, HackSpace magazine, and book projects. He’s a massive sci-fi bore.
Subscribe to Raspberry Pi Official Magazine
Save up to 37% off the cover price and get a FREE Raspberry Pi Pico 2 W with a subscription to Raspberry Pi Official Magazine.
More articles
Make your RAM go further – Raspberry Pi OS memory optimisation tips
In issue 164 of Raspberry Pi Official Magazine we have been playing around with the new Raspberry Pi 5 1GB RAM. While the RAM shortage caused by the demands of AI infrastructure is annoying beyond belief, this has been a great chance for us to really get to grips with RAM. Generating images in Stable […]
Read more →
Mighty Projects – 1GB Computer in Raspberry Pi Official Magazine 164
It’s normal for computers to get faster and more pwerful, but the new-ish Raspberry Pi 5 1GB is a step in the other direction: it has all the processing power and the same GPIO pins of its more costly siblings, but with only 1GB of RAM it’s at a price that’s friendlier on the wallet […]
Read more →
Win one of five 256GB Raspberry Pi Flash Drives
If you’ve been around long enough, you know that every Raspberry Pi accessory is top quality, and the latest Flash Drive is no different. Fancy a big one? We have five up for grabs, and you can enter below… Win 1 of 5 256GB Raspberry Pi Flash Drives
Read more →