Prime numbers explained: how to find them and check them
Prime numbers are the building blocks of all whole numbers, and they protect much of the internet. The idea is simple, but a few details trip people up, starting with whether 1 counts.
The definition
A prime number is a whole number greater than 1 that can be divided exactly only by 1 and by itself. The first primes are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
A number with more than two divisors is called composite. For example 12 can be divided by 1, 2, 3, 4, 6 and 12.
Two details that cause confusion
- The number 1 is not prime. A prime must have exactly two divisors, and 1 has only one. Excluding it also keeps an important rule tidy, described below.
- The number 2 is prime, and it is the only even prime, because every other even number can be divided by 2.
Why primes matter
Every whole number above 1 is either prime or can be written as a product of primes in exactly one way, apart from the order. 60, for example, is 2 x 2 x 3 x 5. This is called the fundamental theorem of arithmetic, and it is why 1 is left out: if 1 counted, you could add 1 x 1 x 1 to any product and break the uniqueness.
Modern encryption relies on the fact that multiplying two very large primes is easy, while finding them again from the product is extremely hard.
Checking a single number
To test whether a number n is prime, try dividing it by every prime up to its square root. You can stop there, because if n has a divisor larger than its square root, it must also have a matching one smaller.
- If n is less than 2, it is not prime.
- If n is 2 or 3, it is prime.
- If n is divisible by 2 or 3, it is not prime.
- Try dividing by 5, 7, 11, 13 and so on up to the square root of n.
- If nothing divides it exactly, n is prime.
The sieve of Eratosthenes
To list all primes up to a limit, write out the numbers and cross out multiples. The method is over two thousand years old and still one of the fastest for ranges.
- Write the numbers from 2 up to your limit.
- Take the first number not crossed out, which is prime.
- Cross out all its multiples, starting from its square.
- Repeat with the next number that is still standing.
How many primes are there?
There are infinitely many, a fact proved by Euclid. They thin out as numbers grow. Counting the primes up to each limit gives:
- The 100th prime is 541 and the 1,000th is 7,919.
- Twin primes are pairs that differ by 2, such as 11 and 13, or 41 and 43.
up to 100 25
up to 1,000 168
up to 10,000 1,229
up to 1,000,000 78,498Try it
The prime number generator lists every prime in a range, finds the prime at a given position and checks a single number, showing the next and previous primes. To break a number into its prime parts, use the prime factorization calculator.