Chapter 2: Prime Numbers and Prime Factorization

The Fundamental Theorem of Arithmetic

Introduction (high-school level)

What is a prime number?

Definition: prime numbers and composite numbers

A natural number $p$ greater than 1 is called a prime number if the only positive divisors of $p$ are $1$ and $p$.

A natural number greater than 1 that is not prime is called a composite number.

Note. $1$ is neither prime nor composite; it is treated as a special case.

Classification of the numbers from 1 to 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 prime composite 1 (special) Primes up to 30: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 (10 primes)

Sieve of Eratosthenes

A classical algorithm for finding all primes within a given range is the Sieve of Eratosthenes. It is attributed to Eratosthenes, a 3rd-century BC Greek mathematician.

Algorithm: Sieve of Eratosthenes

Procedure for finding all primes up to $N$:

  1. Prepare a list of integers from $2$ to $N$.
  2. Record the smallest unprocessed number $p$ in the list as a prime.
  3. Remove $p^2, p^2+p, p^2+2p, \ldots$ (the multiples of $p$ that are at least $p^2$) from the list.
  4. Repeat steps 2 and 3 until $p^2 > N$.
  5. The numbers remaining in the list are exactly the primes.

Why start from $p^2$? Among the multiples of $p$, those of the form $p \cdot k$ with $k < p$ have already been removed at the stage of the earlier prime $k$, so there is no need to remove multiples below $p^2$ again.

Figure 1. The Sieve of Eratosthenes in action ($N = 200$)

The figure below animates the execution of the Sieve of Eratosthenes on the integers from $2$ to $200$. At each stage, multiples of the prime $p$ (those at least $p^2$) are removed, and only the primes remain in the end. Since $\sqrt{200} \approx 14.1$, the procedure completes in 6 steps with $p = 2, 3, 5, 7, 11, 13$.

The infinitude of primes

Theorem (Euclid): there are infinitely many primes.

Proof (by contradiction).

Suppose, for contradiction, that there are only finitely many primes and let $p_1, p_2, \ldots, p_n$ be all of them.

Consider the number $N$ defined by

$$N = p_1 \cdot p_2 \cdot p_3 \cdots p_n + 1$$

Since $N > 1$, it is divisible by some prime $p$.

However, dividing $N$ by $p_1$ gives $N = p_1 \cdot (p_2 \cdots p_n) + 1$, so the remainder is always $1$. The same is true when dividing by any of $p_2, p_3, \ldots, p_n$: the remainder is $1$.

In other words, $N$ is not divisible by any of the primes in our list. Hence the prime $p$ that divides $N$ differs from every $p_1, p_2, \ldots, p_n$.

This contradicts the assumption that $p_1, \ldots, p_n$ are all the primes.

Therefore, there are infinitely many primes. $\square$

The idea of Euclid's proof All primes (assumed) Construct a new number N is not divisible by any prime (remainder 1) → a new prime exists! → contradiction

Euclid's lemma

To prove the Fundamental Theorem of Arithmetic, we first establish an important lemma.

Lemma (Euclid's lemma).

If $p$ is prime and $p$ divides the product $ab$, then $p$ divides $a$ or $p$ divides $b$.

Proof.

Assume $p \mid ab$ but $p \nmid a$ ($p$ does not divide $a$). Since $p$ is prime, the greatest common divisor of $p$ and $a$ is $\gcd(p, a) = 1$ (the divisors of $p$ are only $1$ and $p$, and $p$ does not divide $a$).

Since $\gcd(p, a) = 1$, there exist integers $s, t$ such that

$$ps + at = 1$$

holds. This is known as Bézout's identity, and $s, t$ can be computed explicitly by reversing the steps of the Euclidean algorithm (see Euclidean algorithm for details). Multiplying both sides by $b$,

$$pbs + abt = b$$

The first term $pbs$ on the left is divisible by $p$. The second term $abt$ is also divisible by $p$ since $p \mid ab$. Hence the right-hand side $b$ is also divisible by $p$. $\square$

The Fundamental Theorem of Arithmetic

Theorem (Fundamental Theorem of Arithmetic).

Every natural number $n$ greater than 1 can be expressed as a product of prime numbers.

Moreover, the expression is unique up to the order of the prime factors.

$$n = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k} \quad (p_1 < p_2 < \cdots < p_k, \, e_i \geq 1)$$

Proof (existence).

We prove by induction on $n > 1$.

When $n = 2$: $2$ is prime, so it is itself its prime factorization.

Inductive hypothesis (up to $n - 1$): assume the statement holds for every integer between $2$ and $n-1$ inclusive.

  • If $n$ is prime, then $n$ itself is the prime factorization.
  • If $n$ is composite, then $n = ab$ for some $1 < a, b < n$.

By the inductive hypothesis, both $a$ and $b$ have prime factorizations.

Multiplying them yields a prime factorization of $n$. $\square$

Proof (uniqueness).

Suppose $n$ admits two prime factorizations:

$$n = p_1 p_2 \cdots p_r = q_1 q_2 \cdots q_s$$

Since $p_1$ divides the left-hand side, it also divides the right-hand side $q_1 q_2 \cdots q_s$. Repeated application of Euclid's lemma shows that $p_1$ divides some $q_j$. Since $q_j$ is prime, $p_1 = q_j$.

Canceling $p_1 = q_j$ from both sides and repeating the same argument, one eventually shows that all prime factors agree (up to order). $\square$

Factorization tree of 60
Figure 2: Prime factorization tree of $60$. Repeatedly factor each composite number (blue circles) until only primes (green circles) remain.

Applications of prime factorization

Number and sum of divisors

If $n = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}$, then

Number of divisors: $\tau(n) = (e_1 + 1)(e_2 + 1) \cdots (e_k + 1)$

Sum of divisors: $\sigma(n) = \dfrac{p_1^{e_1+1}-1}{p_1-1} \cdot \dfrac{p_2^{e_2+1}-1}{p_2-1} \cdots \dfrac{p_k^{e_k+1}-1}{p_k-1}$

Proof (number of divisors).

Each divisor of $n$ has the form $d = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}$ with $0 \leq a_i \leq e_i$.

Each $a_i$ has $(e_i + 1)$ choices, namely $0, 1, 2, \ldots, e_i$.

Therefore, the number of divisors equals $(e_1 + 1)(e_2 + 1) \cdots (e_k + 1)$. $\square$

Proof (sum of divisors).

The sum of divisors of $n$ is the sum over all divisors $d = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}$ with $0 \leq a_i \leq e_i$, so

$$\sigma(n) = \sum_{a_1=0}^{e_1} \sum_{a_2=0}^{e_2} \cdots \sum_{a_k=0}^{e_k} p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}$$

Since the summations over different primes $p_i$ are independent, the multiple sum factors as a product:

$$= \left(\sum_{a_1=0}^{e_1} p_1^{a_1}\right) \left(\sum_{a_2=0}^{e_2} p_2^{a_2}\right) \cdots \left(\sum_{a_k=0}^{e_k} p_k^{a_k}\right)$$

Each factor is a geometric series:

$$\sum_{a=0}^{e} p^a = 1 + p + p^2 + \cdots + p^e = \dfrac{p^{e+1} - 1}{p - 1}$$

Therefore,

$$\sigma(n) = \dfrac{p_1^{e_1+1}-1}{p_1-1} \cdot \dfrac{p_2^{e_2+1}-1}{p_2-1} \cdots \dfrac{p_k^{e_k+1}-1}{p_k-1} \qquad \square$$

Example: divisors of $60 = 2^2 \cdot 3 \cdot 5$

Number of divisors: $(2+1)(1+1)(1+1) = 3 \cdot 2 \cdot 2 = 12$

Sum of divisors: $\dfrac{2^3-1}{2-1} \cdot \dfrac{3^2-1}{3-1} \cdot \dfrac{5^2-1}{5-1} = 7 \cdot 4 \cdot 6 = 168$

List of divisors: $1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60$ (sum $= 168$ ✓)

GCD and LCM via prime factorization

Suppose the prime factorizations of two natural numbers $a$ and $b$ are

$$a = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}, \qquad b = p_1^{b_1} p_2^{b_2} \cdots p_k^{b_k}$$

(use exponent $0$ for primes that do not appear in one of them). Then

Greatest common divisor: $\gcd(a, b) = p_1^{\min(a_1, b_1)} p_2^{\min(a_2, b_2)} \cdots p_k^{\min(a_k, b_k)}$

Least common multiple: $\operatorname{lcm}(a, b) = p_1^{\max(a_1, b_1)} p_2^{\max(a_2, b_2)} \cdots p_k^{\max(a_k, b_k)}$

Intuitively, the GCD takes the "smaller exponent" for each prime, and the LCM takes the "larger exponent."

Example: GCD and LCM of $48$ and $72$

Since $48 = 2^4 \times 3^1$ and $72 = 2^3 \times 3^2$,

$\gcd(48, 72) = 2^{\min(4,3)} \times 3^{\min(1,2)} = 2^3 \times 3 = 24$

$\operatorname{lcm}(48, 72) = 2^{\max(4,3)} \times 3^{\max(1,2)} = 2^4 \times 3^2 = 144$

Check: $\gcd \times \operatorname{lcm} = 24 \times 144 = 3456 = 48 \times 72$ ✓

Cryptographic applications

Primes and prime factorization form the foundation of public-key cryptography (such as RSA), which underpins modern internet security.

The security of RSA relies on the following fact:

  • computing the product $n = pq$ of two large primes $p$ and $q$ is easy;
  • but recovering $p$ and $q$ from $n$ alone (i.e., factoring $n$) is very hard.

This asymmetry — multiplication is easy but the inverse is hard — guarantees the security of the cipher. For details, see:

Exercises

Exercise 1

Find the prime factorization of $252$ and the number of divisors.

Solution

$252 = 2 \times 126 = 2 \times 2 \times 63 = 4 \times 63 = 4 \times 9 \times 7 = 2^2 \times 3^2 \times 7$

Number of divisors: $(2+1)(2+1)(1+1) = 3 \times 3 \times 2 = 18$

Listing all 18 divisors with their prime factorizations (all written in the uniform form $2^a \cdot 3^b \cdot 7^c$):

Divisor $a$ $b$ $c$ $2^a \cdot 3^b \cdot 7^c$
$1$000$2^0 \cdot 3^0 \cdot 7^0$
$2$100$2^1 \cdot 3^0 \cdot 7^0$
$3$010$2^0 \cdot 3^1 \cdot 7^0$
$4$200$2^2 \cdot 3^0 \cdot 7^0$
$6$110$2^1 \cdot 3^1 \cdot 7^0$
$7$001$2^0 \cdot 3^0 \cdot 7^1$
$9$020$2^0 \cdot 3^2 \cdot 7^0$
$12$210$2^2 \cdot 3^1 \cdot 7^0$
$14$101$2^1 \cdot 3^0 \cdot 7^1$
$18$120$2^1 \cdot 3^2 \cdot 7^0$
$21$011$2^0 \cdot 3^1 \cdot 7^1$
$28$201$2^2 \cdot 3^0 \cdot 7^1$
$36$220$2^2 \cdot 3^2 \cdot 7^0$
$42$111$2^1 \cdot 3^1 \cdot 7^1$
$63$021$2^0 \cdot 3^2 \cdot 7^1$
$84$211$2^2 \cdot 3^1 \cdot 7^1$
$126$121$2^1 \cdot 3^2 \cdot 7^1$
$252$221$2^2 \cdot 3^2 \cdot 7^1$

$a$ has 3 choices ($0, 1, 2$), $b$ has 3 choices ($0, 1, 2$), and $c$ has 2 choices ($0, 1$), giving $3 \times 3 \times 2 = 18$ combinations, which correspond to exactly the 18 divisors.

Exercise 2

Compute $\gcd(48, 72)$ and $\operatorname{lcm}(48, 72)$ using prime factorization.

Solution

From $48 = 2^4 \times 3$ and $72 = 2^3 \times 3^2$,

$\gcd(48, 72) = 2^{\min(4,3)} \times 3^{\min(1,2)} = 2^3 \times 3 = 24$

$\operatorname{lcm}(48, 72) = 2^{\max(4,3)} \times 3^{\max(1,2)} = 2^4 \times 3^2 = 144$

Frequently Asked Questions

What is a prime number?

A prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. The smallest prime is 2, and 2 is the only even prime. The sequence continues 3, 5, 7, 11, 13, …, indefinitely.

Are there infinitely many primes?

Yes, there are infinitely many primes. This was proved by Euclid around 300 BC by contradiction. Assuming only finitely many primes $p_1, \ldots, p_n$, the number obtained by adding 1 to their product is not divisible by any of them, which is a contradiction.

What is the Fundamental Theorem of Arithmetic?

It states that every natural number greater than 1 can be expressed as a product of primes, and the expression is unique up to the order of the prime factors. For example, $60 = 2^2 \times 3 \times 5$, and this prime factorization is the only one.