Tuesday, October 25, 2016

-----
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. Well, those days are over.
----
A prime number is any positive whole number that can only get evenly divided only by 1 and itself. Primes are used in many applications; a popular use being for encryption and cryptography. Demonstrated here is another use for prime numbers. That is making use of an older/slower Raspberry PI and a few parts to nerd up the decor of any room.

The project has an entertaining audio effect if you are into numbers. Primes go on forever and ever; infinitely large. The smallest numerical difference between two primes is 2 (example: 7-5=2). What is interesting is the distance (difference) between two consecutive primes stays relatively low as the primes become very large. Press a button and Primes in a Box gives a 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 straight forward. 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 Raspbery PI project to be powered down gracefully if it needs to be moved.

-----
You'll need a Raspberry PI, 5VDC relay, PN2222A transistor, 16x2 I2C LCD, two resistors, and two normally open button switches. A project box holds it all together. Connect it all up like this:

Once on the breadboard it will look a bit like this:

-----
Set up the RasPI to run the python code below at reboot (use @reboot in the sudo crontab).
Note, two files are expected to be found in the working directory:
- prime_list.txt (List of prime numbers in order. One per line. As many as you like)
- high_prime.txt (Holds the highest prime found in case of a restart/reboot)