What a prime number is and why you might need to find them
A prime number is a whole number larger than 1 that can only be divided evenly by 1 and itself. The number 7 is prime because nothing except 1 and 7 divides into it without a remainder. The number 6 is not prime because 2 and 3 both divide into it evenly. Finding primes matters in cryptography (the math behind find passwords), in certain computer algorithms, and sometimes just as a mental exercise or programming challenge.
You can find primes by hand using a straightforward method, or you can write code to do it faster. The method you choose depends on how many primes you need and how large they are.
Key Takeaways
- A prime number is a whole number larger than 1 that only divides evenly by 1 and itself.
- The simplest hand method is trial division: test whether each candidate number divides evenly by any number between 2 and its square root.
- The Sieve of Eratosthenes is faster for finding all primes up to a specific number, and works by crossing out multiples of each prime you find.
- In code, trial division works well for checking single numbers, while a sieve works better when you need a long list of primes.
- For very large numbers, specialized algorithms like the Miller-Rabin test exist, but trial division and sieves cover most everyday needs.
Trial division: testing one number at a time
The most straightforward way to check whether a single number is prime is trial division. You test whether the number divides evenly by any smaller number. If it does, it is not prime. If nothing divides it evenly, it is prime.
Here is the process by hand. Let's check whether 17 is prime. You only need to test divisors up to the square root of 17, which is about 4.1. So test 2, 3, and 4. Does 17 divide evenly by 2? No (17 ÷ 2 = 8.5). Does 17 divide evenly by 3? No (17 ÷ 3 = 5.67). Does 17 divide evenly by 4? No (17 ÷ 4 = 4.25). Since nothing divides it, 17 is prime.
Why only test up to the square root? Because if a number has a factor larger than its square root, it must also have a factor smaller than its square root. Testing up to the square root catches all possible factors, so you can stop there.
For larger numbers by hand, this gets tedious quickly. But in code, a computer can test hundreds of divisors in a fraction of a second. Trial division is the fastest method when you are checking whether a single number is prime.
The Sieve of Eratosthenes: finding many primes at once
If you need to find all primes up to a certain number—say, all primes up to 100—trial division becomes slow because you repeat the same work. The Sieve of Eratosthenes is much faster for this task. It works by elimination: you write down all numbers in your range, then cross out multiples of each prime you find.
Here is how it works. Say you want all primes up to 30. Write the numbers 2 through 30. Start with 2 (the first prime). Cross out all multiples of 2: 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30. Move to the next uncrossed number, which is 3. Cross out all multiples of 3 that are not already crossed: 9, 15, 21, 27. Move to the next uncrossed number, which is 5. Cross out all multiples of 5: 25. The next uncrossed number is 7, but 7 squared is 49, which is larger than 30, so you can stop. The uncrossed numbers are your primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.
The sieve is much faster than trial division when you need many primes, because you do the work once and get all results at the end. In code, a sieve can find all primes up to a million in under a second on a modern computer.
Trial division in code
Here is a straightforward trial division function in Python. It tests whether a single number is prime by checking divisors up to its square root:
def is_prime(n): if n < 2: return False if n == 2: return True if n % 2 == 0: return False for i in range(3, int(n**0.5) + 1, 2): if n % i == 0: return False return True
This function returns True if n is prime and False if it is not. It first handles edge cases (numbers less than 2 are not prime, and 2 is prime). Then it checks whether n is even. If it is, it is not prime (except for 2, which we already handled). Finally, it loops through odd numbers from 3 up to the square root of n and tests whether any divide n evenly. If one does, the function returns False. If the loop finishes without finding a divisor, the function returns True.
You can call this function on any number: is_prime(17) returns True, and is_prime(18) returns False. For numbers up to a few million, this runs almost when ready.
The Sieve of Eratosthenes in code
Here is a sieve implementation in Python that finds all primes up to a given number:
def sieve_of_eratosthenes(limit): is_prime = [True] * (limit + 1) is_prime[0] = is_prime[1] = False for i in range(2, int(limit**0.5) + 1): if is_prime[i]: for j in range(i*i, limit + 1, i): is_prime[j] = False return [i for i in range(2, limit + 1) if is_prime[i]]
This function creates a list of True values, one for each number up to the limit. It marks 0 and 1 as False (not prime). Then it loops through each number from 2 up to the square root of the limit. If a number is still marked True, it marks all multiples of that number (starting from the number squared) as False. At the end, it returns all numbers still marked True. Calling sieve_of_eratosthenes(30) returns [2, 3, 5, 7, 11, 13, 17, 19, 23, 29].
When to use each method
Use trial division when you need to check whether a single number is prime, or when you need to check a few scattered numbers. It is fast and uses very little memory. Use the sieve when you need all primes up to a certain number, or when you need a long list of primes. The sieve is much faster for this task because it avoids repeating work.
For very large numbers (hundreds of digits), specialized algorithms like the Miller-Rabin test exist, but they are more complex to implement. Trial division and the sieve cover most everyday programming and math needs.
Frequently Asked Questions
Is 1 a prime number?
No. By definition, a prime number must be larger than 1 and divisible only by 1 and itself. The number 1 only has one divisor (itself), so mathematicians exclude it from the primes.
Is 2 the only even prime number?
Yes. Every even number except 2 is divisible by 2, so it has at least three divisors: 1, 2, and itself. That makes all even numbers except 2 composite (not prime).
How do I find very large prime numbers?
Trial division becomes slow for numbers with hundreds of digits. Specialized tests like Miller-Rabin can check whether a number is probably prime much faster, though they do not may provide certainty. For cryptography, libraries like OpenSSL handle this automatically.
Why does the sieve only need to check up to the square root?
If a number n has a factor larger than its square root, it must also have a factor smaller than its square root (because the two factors multiply to n). So checking up to the square root finds all factors. Once you pass the square root, any remaining unmarked numbers are prime.
Can I use these methods to find prime numbers for encryption?
These methods work for learning how primes are found, but encryption uses much larger primes (hundreds of digits) and specialized algorithms. Use a cryptography library like Python's cryptography module rather than writing your own prime-finding code for security purposes.