Illustrated · 3 min
I know - testing for primality is a pain. It's usually a time-intensive process that requires a lot of scratch paper if you're doing it out by hand. But guess what? It's a pain for computers too! Okay, maybe they're a little faster than us, but a lot of important cybersecurity protocols depend upon the fact that computers are as bad at testing for primality as we are!
Unfortunately for both humans and computers, the most efficient way of testing for primality is by trial division. Trial division is exactly what it sounds like. Say you have the number 103. Trial division involves taking all the numbers - 1, 2, 3, 4, 5, etc. - up to 103 and checking if they divide into 103. Does that sound slow? That's because it is slow. You can save time by only testing primes up to the smallest integer that's larger than , where n is the number that we're testing for primality. Why does this work? Well, after , the factors just start repeating. For example, let's look at the factor pairs of 100:
(1, 100) (2, 50) (4, 25) (5, 20) (10, 10) (20, 5) (25, 4) (50, 2) (100, 1)
After 10 and 10, they all start repeating! We don't need to test anything past 10, which is the square root of 100.
If you're programming a computer to test for primes, you can basically do it the same way. The easiest way is to give the computer a list of primes to test with. That way, if n is divisible by any of the primes less than , the computer can say the number is composite and skip testing any other numbers.
But how does the computer find the list of prime numbers? Obviously, it's a pain to put them all in manually. So it uses a nifty little algorithm represented here:

Sieve of Eratosthenes
What the computer is doing is it's starting from the smallest prime number, 2. It marks 2 as prime p. Then it goes and marks all the other numbers that are divisible by 2 as composite. After it's finished, it moves onto 3 and marks it as the new prime p. Then it marks all the multiples of 3 as composite and moves onto the next number. When the algorithm terminates, all the prime numbers should be marked.
The computer can do this pretty quickly for numbers up to 1000 or 10000 - way faster than we can! However, it's not invincible. Once you start making the upper bound really big, the computer starts to take a little longer to find all of the primes. And even if it has a really big list of primes, say you ask the computer to determine if a number like:
15226050279225333605356183781326374297180681149613 80688657908494580122963258952897654000350692006139
is prime. We certainly can't do this practically, and believe it or not, it takes the computer forever as well! It has to first determine all the primes up to the square root of that number and then test all the primes to see if they divide into that number (which has a 100 digits, so it's square root is still super big). This number is called a semiprime, which means it is the product of two other prime numbers. It's factorization is:
37975227936943673922808872755445627854565536638199 × 40094690950920881030683735292761468389214899724061.
This took a parallel computer a few days to solve! And this isn't even a very big semiprime!
Why is this important, you might wonder? Well, the most widely used public-key cryptosystem today is called RSA. The core idea behind breaking the RSA encryption (more commonly known as the RSA problem) is based on multiplying two extremely large prime numbers to form a semiprime, which is used as a public key to encrypt data transmissions. If you don't know the private key, which is one of the two large prime numbers that are factors of the semiprime, it takes computers a really long time to find it by itself! That's why this cryptosystem is so simple, and yet so frustrating to computers.
For now, prime numbers remain as annoying to computers as they are to us! Who knew that we could find such a simple way to trick them?
For more information, visit the expii article about Crytography and RSA encryption!