Primes and Prime Factorization
A prime number is a whole number greater than 1 whose only factors are 1 and itself: 2, 3, 5, 7, 11, 13, … Every other whole number above 1 can be built by multiplying primes together, in exactly one way. That makes primes the atoms of arithmetic, and it makes them the foundation of the encryption that protects every website you visit.
Prime or composite?
| Kind | Definition | Examples |
|---|---|---|
| prime | exactly two factors: 1 and itself | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 |
| composite | more than two factors | , , |
| neither | 0 and 1 |
Notes on the edges:
- 1 is not prime. It has only one factor. Excluding it keeps the "exactly one way" rule below true (otherwise ).
- 2 is the only even prime. Every other even number has 2 as a factor.
To test whether is prime, try dividing by up to . If none divides it, is prime. As in Factors and Multiples, any factor above would pair with one below it, which you would already have found. Is 221 prime? ; testing 2, 3, 5, 7, 11, 13 finds . So no.
import math
def is_prime(n):
if n < 2:
return False
for d in range(2, math.isqrt(n) + 1):
if n % d == 0:
return False
return True
for n in [1, 2, 9, 97, 221, 1009]:
print(n, is_prime(n))1 False
2 True
9 False
97 True
221 False
1009 True
The sieve of Eratosthenes
To find all primes up to some limit, an ancient method beats testing each number. Write the numbers 2 to . Circle 2 and cross out all its multiples. The next uncrossed number (3) is prime: cross out its multiples. Repeat. Whatever survives is prime.
def sieve(limit):
is_p = [True] * (limit + 1)
is_p[0] = is_p[1] = False
for p in range(2, int(limit ** 0.5) + 1):
if is_p[p]:
for multiple in range(p * p, limit + 1, p):
is_p[multiple] = False
return [n for n in range(limit + 1) if is_p[n]]
primes = sieve(100)
print(primes)
print(len(primes), "primes below 100;", len(sieve(10000)), "below 10 000")[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
25 primes below 100; 1229 below 10 000
Crossing out starts at because smaller multiples of (such as , ) were already crossed out by smaller primes.
Primes thin out as numbers grow (25 of the first 100 numbers, about 12% of the first 10 000) but they never stop. Euclid's proof is short: suppose there were a last prime. Multiply all the primes together and add 1. That new number leaves remainder 1 when divided by every prime on the list, so its prime factors are not on the list. The list was not complete after all.
Prime factorization
The Fundamental Theorem of Arithmetic says every whole number above 1 is a product of primes in exactly one way (ignoring the order). Finding it is called prime factorization. A factor tree splits the number until only primes are left:
360
/ \
36 10
/ \ / \
6 6 2 5
/ \ / \
2 3 2 3Any split works; the tree may look different, but the primes at the bottom are always the same. Writing repeated primes with exponents ( means ) keeps it short (see Exponents).
In code, divide by the smallest prime as often as it goes, then move to the next divisor:
def factorize(n):
result = {}
d = 2
while d * d <= n:
while n % d == 0:
result[d] = result.get(d, 0) + 1
n //= d
d += 1
if n > 1: # whatever is left is a prime
result[n] = result.get(n, 0) + 1
return result
def show(f):
return " x ".join(f"{p}^{e}" if e > 1 else str(p) for p, e in f.items())
for n in [360, 97, 1001, 1024, 123456]:
print(n, "=", show(factorize(n)))360 = 2^3 x 3^2 x 5
97 = 97
1001 = 7 x 11 x 13
1024 = 2^10
123456 = 2^6 x 3 x 643
SymPy does the same with sympy.factorint(360), which returns {2: 3, 3: 2, 5: 1}.
The factorization tells you everything about a number's factors. has factors, because each factor picks 0–3 twos, 0–2 threes and 0–1 fives. It also makes GCF and LCM easy, and it is how fractions are simplified.
In software
- Cryptography: RSA encryption multiplies two huge primes (hundreds of digits each). Multiplying is instant; factoring the product back is so slow that nobody can do it in practice. That one-way difficulty keeps the private key secret (see Asymmetric Encryption).
- Hash tables often use a prime number of buckets, so keys with patterns (all multiples of 4, say) still spread evenly.
- Game design and animation: layering cycles with prime lengths (7, 11, 13 frames) makes repeating patterns line up rarely, so motion looks less mechanical.
- Interview classics: "is this prime?" and "sieve up to " are standard warm-up problems.
Exercises
Exercise 1: Prime or not?
Decide which are prime: 51, 53, 87, 91, 101.
Answer
53 and 101 are prime; 51, 87 and 91 are not.
Solution
Exercise 2: Factor tree
Find the prime factorization of 1 200.
Answer
Solution
Exercise 3: Count the factors
How many factors does 1 200 have? Use its factorization, then check with code.
Answer
30
Solution
With , a factor picks 0–4 twos, 0–1 threes and 0–2 fives: .
print(sum(1 for d in range(1, 1201) if 1200 % d == 0))30
Exercise 4: Twin primes
Twin primes are pairs of primes that differ by 2, like (11, 13). Use a sieve to list all twin-prime pairs below 100.
Answer
(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73): 8 pairs.
Solution
Sieve once, then check each prime for whether is also prime.
limit = 100
is_p = [True] * (limit + 1)
is_p[0] = is_p[1] = False
for p in range(2, 11):
if is_p[p]:
for m in range(p * p, limit + 1, p):
is_p[m] = False
twins = [(p, p + 2) for p in range(2, limit - 1) if is_p[p] and is_p[p + 2]]
print(twins, len(twins))[(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73)] 8
Whether there are infinitely many twin primes is still unproven.
Test yourself
Which of these is prime?
- 59
- 1
- 57
- 91
What is the prime factorization of 84?
To test whether 527 is prime, which divisors must you try?
- Every number up to 527
- Every number up to 263
- Primes up to 22 (since )
- Only 2, 3 and 5