DISCRETE MATH • NUMBER THEORY AND CRYPTOGRAPHY

Prime numbers and factorization concepts

The indivisible atoms of arithmetic that secure modern digital communication.

Historical Context & Motivation

The study of prime numbers is among the oldest pursuits in mathematics, stretching back more than two millennia to the ancient Greeks who first recognized that certain integers resist decomposition into smaller factors. The question that animated early number theorists — which numbers are truly indivisible, and how do they compose every other integer? — remains central to modern discrete mathematics and forms the computational backbone of public-key cryptography. Understanding why primes are easy to multiply together yet extraordinarily difficult to recover from their product is the asymmetry upon which protocols like RSA depend.

~300 BCE
Euclid's Elements
Euclid proves the infinitude of primes in Book IX, Proposition 20 — one of the earliest known proofs by contradiction — and establishes the Fundamental Theorem of Arithmetic in embryonic form, asserting unique factorization into primes.
~240 BCE
Sieve of Eratosthenes
Eratosthenes devises a systematic algorithm for enumerating all primes up to a given bound by iteratively eliminating composite multiples — a procedure still taught and used as a baseline primality-enumeration method.
1801
Gauss's Disquisitiones Arithmeticae
Carl Friedrich Gauss publishes a rigorous treatment of modular arithmetic, quadratic residues, and the Fundamental Theorem of Arithmetic, establishing number theory as a formal mathematical discipline.
1977
RSA Cryptosystem
Rivest, Shamir, and Adleman publish the RSA algorithm, converting the computational hardness of integer factorization into a practical public-key encryption scheme that secures internet communications worldwide.
2002–present
AKS and Post-Quantum Research
Agrawal, Kayal, and Saxena prove that primality testing is in P. Meanwhile, Shor's quantum algorithm threatens factorization-based cryptography, spurring research into lattice-based and post-quantum alternatives.

The central question that links ancient Greek curiosity to twenty-first-century cybersecurity is deceptively simple: given a large integer n, can we efficiently determine whether it is prime, and if not, can we recover its prime factors? The gap between answering the first question (polynomial time) and the second (no known classical polynomial-time algorithm for general composites) is precisely the asymmetry that modern cryptographic systems exploit.

Core Principles & Definitions

Before exploring algorithms and applications, we must establish the foundational definitions and theorems that govern the arithmetic of primes. These principles form the axiomatic scaffolding for everything from elementary divisibility arguments to sophisticated cryptographic protocols. A clear grasp of divisibility, primality, and unique factorization is essential for the rigorous reasoning that discrete mathematics demands.

1

Prime & Composite

An integer p > 1 is prime if its only positive divisors are 1 and p. Otherwise it is composite. The integer 1 is neither prime nor composite by convention.
2

Fundamental Theorem of Arithmetic

Every integer n > 1 can be expressed as a product of primes in exactly one way, up to the order of the factors. This unique factorization is the cornerstone of multiplicative number theory.
3

Divisibility & the Division Algorithm

For integers a and b with b > 0, there exist unique integers q (quotient) and r (remainder) such that a = bq + r, 0 ≤ r < b. Divisibility (a | b) means r = 0.
4

GCD & Euclid's Algorithm

The greatest common divisor gcd(a, b) is the largest integer dividing both a and b. Euclid's algorithm computes it efficiently via repeated application of the division algorithm: gcd(a, b) = gcd(b, a mod b).
5

Infinitude of Primes

There are infinitely many primes. Euclid's classic proof assumes a finite list {p₁, …, pₖ}, constructs N = p₁p₂…pₖ + 1, and shows N must have a prime factor outside the list — a contradiction.
KEY TAKEAWAY
Think of primes as the chemical elements of the integers. Just as every molecule can be decomposed into a unique combination of atoms from the periodic table, every integer greater than 1 decomposes into a unique product of primes. The Fundamental Theorem of Arithmetic is the "conservation law" that guarantees this decomposition is well-defined — there is no alternative factorization lurking in the background, which is precisely why factorization-based cryptography can work at all.

Visualizing the Sieve of Eratosthenes

The Sieve of Eratosthenes provides both an intuitive visualization of how primes are identified and a concrete algorithm with well-understood time complexity. The procedure begins with all integers from 2 to some bound n marked as potentially prime. Starting with the smallest unmarked number (2), the algorithm eliminates all of its proper multiples, then advances to the next surviving number, repeating until it has processed every integer up to √n. The diagram below illustrates this process for integers 2 through 50.

The grid shows integers 2–50 after running the Sieve of Eratosthenes. Colored cells are primes that survived the sieve; faded cells with strikethrough are composites eliminated as multiples of 2, 3, 5, or 7. Because √50 ≈ 7.07, no further sieving is needed beyond 7.

Observe that once the multiples of 2 are eliminated, roughly half the candidates vanish; crossing out multiples of 3 removes another significant fraction, and so on. The key efficiency insight is that the sieve only needs to process primes up to √n, because any composite number m ≤ n must have a prime factor no larger than √m ≤ √n. The sieve's time complexity is O(n log log n), making it remarkably efficient for generating all primes up to moderate bounds and providing a powerful intuition pump for understanding how primes thin out among the integers.

Mathematical Framework

The theoretical apparatus surrounding primes and factorization rests on several interlocking results. We formalize the key statements and connect them to algorithmic complexity — the bridge between pure number theory and its cryptographic applications.

FUNDAMENTAL THEOREM OF ARITHMETIC
n = p₁^{a₁} × p₂^{a₂} × … × pₖ^{aₖ}
Every integer n > 1 has a unique representation as a product of prime powers, where p₁ < p₂ < … < pₖ are distinct primes and each exponent aᵢ ≥ 1. This factorization is unique up to ordering.
EULER'S TOTIENT FUNCTION
φ(n) = n × ∏_{p | n} (1 − 1/p)
φ(n) counts the integers in {1, …, n} that are coprime to n. For n = p₁^{a₁}…pₖ^{aₖ}, this product formula follows directly from the inclusion-exclusion principle applied to the prime divisors of n. Euler's totient is central to RSA key generation.
PRIME NUMBER THEOREM
π(x) ~ x / ln(x) as x → ∞
π(x) denotes the number of primes not exceeding x. The theorem states that the ratio π(x) / (x / ln x) approaches 1, meaning primes become sparser logarithmically. This informs the expected size of a random prime near a given magnitude and directly influences the key-size estimates used in cryptographic standards.
TRIAL DIVISION COMPLEXITY
O(√n) divisions to test primality of n
The simplest primality test divides n by every integer from 2 to ⌊√n⌋. If no divisor is found, n is prime. While straightforward, this is exponential in the number of digits of n (bit-length b implies √n = 2^{b/2}), which is why it is impractical for cryptographic-scale numbers with hundreds of digits.
Bit-Length vs. Magnitude
A subtle but crucial distinction: when we say trial division is O(√n), that is polynomial in the value of n but exponential in the bit-length b = ⌈log₂ n⌉. In complexity theory and cryptography, input size is measured in bits, so trial division is O(2^{b/2}) — decidedly not efficient. This is why algorithms like the Miller–Rabin probabilistic test (O(k × b² × log b)) are preferred in practice.

Factorization Algorithms & Classification

Factorization algorithms range from elementary procedures suitable for small integers to sophisticated sub-exponential methods designed for cryptographic-scale numbers. The classification below distinguishes algorithms by their asymptotic complexity and practical applicability. Understanding this landscape clarifies both why factorization is considered hard and where the boundaries of current computational capability lie.

Comparison of factorization algorithms by asymptotic complexity. Classical algorithms occupy the exponential to sub-exponential range (GNFS is the fastest known classical method). Shor's quantum algorithm achieves polynomial time in the bit-length, motivating the shift toward post-quantum cryptography.
Summary of major factorization algorithms
AlgorithmComplexity ClassBest ForPractical Limit
Trial DivisionExponential: O(2^{b/2})Small factors, pedagogical clarity~20 digits
Pollard's ρO(n^{1/4}) expectedFinding small-to-medium prime factors~30 digits
Quadratic SieveSub-exponential: L_n[1/2, 1]Integers up to ~100 digits~100 digits
GNFSSub-exponential: L_n[1/3, c]Largest known factorizations~250 digits (current record)
Shor's AlgorithmPolynomial: O(b³)Quantum computers (theoretical)Limited by qubit count

The notation L_n[α, c] = exp(c × (ln n)^α × (ln ln n)^{1−α}) provides a unified way to express sub-exponential complexity. When α = 0 we recover polynomial time, and when α = 1 we recover fully exponential time; the factorization algorithms of greatest practical interest sit at α = 1/3 (GNFS) or α = 1/2 (Quadratic Sieve), occupying the critical middle ground that makes factorization hard but not impossibly so — exactly the sweet spot that RSA exploits by choosing key sizes that push n beyond the reach of the fastest known classical algorithms.

Worked Example: Prime Factorization & Euler's Totient

We now walk through a complete factorization of a composite number and compute Euler's totient function — the precise calculation that appears during RSA key generation. The goal is to connect the theoretical framework to a concrete computation.

Factor n = 2520 and compute φ(2520)
1
Step 1 — Begin with the smallest primeDivide 2520 by 2 repeatedly: 2520 ÷ 2 = 1260, 1260 ÷ 2 = 630, 630 ÷ 2 = 315. Since 315 is odd, we have extracted the full power of 2.
2520 = 2³ × 315
2
Step 2 — Continue with the next prime (3)Divide 315 by 3: 315 ÷ 3 = 105, 105 ÷ 3 = 35. Since 35 is not divisible by 3, we stop.
2520 = 2³ × 3² × 35
3
Step 3 — Continue with 5Divide 35 by 5: 35 ÷ 5 = 7. Since 7 is not divisible by 5, we extract a single factor of 5.
2520 = 2³ × 3² × 5¹ × 7
4
Step 4 — Check if the remaining factor is primeThe remaining factor is 7. Since 7 is prime (√7 ≈ 2.65, and 7 is not divisible by 2), the factorization is complete.
2520 = 2³ × 3² × 5 × 7
5
Step 5 — Compute φ(2520)Apply the totient product formula: φ(2520) = 2520 × (1 − 1/2) × (1 − 1/3) × (1 − 1/5) × (1 − 1/7) = 2520 × (1/2) × (2/3) × (4/5) × (6/7). Computing step by step: 2520 × 1/2 = 1260; 1260 × 2/3 = 840; 840 × 4/5 = 672; 672 × 6/7 = 576.
φ(2520) = 576
6
Step 6 — Interpret the resultOf the 2520 integers from 1 to 2520, exactly 576 are coprime to 2520. In an RSA context, if n = p × q with p and q prime, then φ(n) = (p − 1)(q − 1), and the private key exponent d is chosen such that e × d ≡ 1 (mod φ(n)). The ability to compute φ(n) hinges on knowing the factorization of n — this is precisely the trapdoor.
576 / 2520 ≈ 22.9% of integers ≤ 2520 are coprime to it

Primality Testing: Methods Compared

While factorization remains computationally difficult for large numbers, primality testing — merely determining whether a number is prime without finding its factors — can be done efficiently. This asymmetry is essential: RSA key generation requires finding large primes quickly, even though verifying that their product is hard to factor. The table below compares the major primality tests along several dimensions relevant to both theory and implementation.

Primality testing methods comparison
MethodTypeComplexityLimitations
Trial DivisionDeterministicO(√n) — exponential in bit-lengthImpractical beyond ~20 digits
Fermat TestProbabilisticO(k × b² log b)Fooled by Carmichael numbers (pseudoprimes to all bases)
Miller–RabinProbabilistic (Monte Carlo)O(k × b² log b); error ≤ 4^{−k}Non-zero (but tunable) error probability
AKSDeterministicÕ(b^{6}) — polynomialTheoretically polynomial but slower in practice than Miller–Rabin
KEY TAKEAWAY
Primality testing is to factorization what checking whether a padlock is locked is to picking it open. You can rattle a lock in seconds (test primality in polynomial time), but actually opening it without the key (factoring the number) requires vastly more effort. This computational asymmetry — easy to verify, hard to invert — is the defining property of one-way functions, the cornerstone of public-key cryptography. In practice, Miller–Rabin with 40+ rounds provides overwhelming statistical confidence (error probability < 2^{−80}) and is the standard method used in cryptographic libraries for generating probable primes.

Connection to Cryptography & Advanced Theory

The theoretical hardness of integer factorization translates directly into the security guarantees of the RSA cryptosystem, which remains one of the most widely deployed public-key algorithms. In RSA, a user selects two large primes p and q, publishes n = pq, and keeps the factorization secret. The public key consists of (n, e) and the private key of d, where ed ≡ 1 (mod φ(n)). An adversary who can factor n can compute φ(n) = (p − 1)(q − 1) and thereby recover d; conversely, without the factorization, computing d from (n, e) is believed to be computationally infeasible.

From number theory to cryptographic protocols
ConceptElementary Number TheoryCryptographic Application
Prime factorizationUnique decomposition of integers (FTA)Hardness of factoring n = pq is the RSA security assumption
Euler's totientCounting coprime residues mod nφ(n) = (p−1)(q−1) determines the RSA private exponent d
Modular exponentiationEuler's theorem: a^{φ(n)} ≡ 1 (mod n) for gcd(a,n)=1Encryption: c ≡ m^e (mod n); Decryption: m ≡ c^d (mod n)
Primality testingDetermining if n is prime (polynomial time)Efficient generation of large primes for key pairs
Discrete logarithmFinding x such that g^x ≡ h (mod p)Basis for Diffie–Hellman key exchange and ElGamal encryption

Looking forward, the advent of scalable quantum computing poses a fundamental challenge to factorization-based cryptography. Shor's algorithm factors integers in polynomial time on a quantum computer, rendering RSA insecure once sufficiently large quantum machines exist. This has spurred the development of post-quantum cryptography, including lattice-based schemes (e.g., NTRU, Kyber) and code-based systems whose security rests on problems believed to be hard even for quantum computers. The study of primes and factorization thus remains not merely historically significant but actively evolving, as the mathematical community works to understand which computational assumptions will survive the quantum era.

Practice Problems

PROBLEM 1CONCEPTUAL
Explain why 1 is excluded from the set of prime numbers. What would go wrong with the Fundamental Theorem of Arithmetic if 1 were considered prime?
PROBLEM 2BASIC CALCULATION
Find the prime factorization of 1764 and compute φ(1764) using Euler's product formula.
PROBLEM 3INTERMEDIATE
Using the Euclidean algorithm, compute gcd(252, 198). Then express gcd(252, 198) as a linear combination of 252 and 198 via back-substitution (the Extended Euclidean Algorithm).
PROBLEM 4APPLIED
In a toy RSA setup, let p = 61 and q = 53. Compute n = pq, φ(n), and find the private exponent d given the public exponent e = 17. Encrypt the plaintext message m = 65 and then decrypt the resulting ciphertext to verify correctness.
PROBLEM 5CRITICAL THINKING
Prove that if p is prime and p | ab, then p | a or p | b (Euclid's Lemma). Then explain why this lemma is essential for establishing the uniqueness part of the Fundamental Theorem of Arithmetic.

Lesson Summary

This lesson established that prime numbers are the multiplicative building blocks of the integers, formalized by the Fundamental Theorem of Arithmetic, which guarantees unique factorization of every integer greater than 1. We traced the subject from Euclid's ancient proof of infinitude through the Sieve of Eratosthenes to the Prime Number Theorem, which describes the asymptotic density of primes. Key tools include Euler's totient function φ(n), the Euclidean algorithm for computing greatest common divisors, and Euclid's Lemma, which underpins the uniqueness proof of the FTA.

On the computational side, we distinguished between primality testing (efficiently solvable via Miller–Rabin or deterministic AKS) and integer factorization (for which no classical polynomial-time algorithm is known). This asymmetry is the foundation of the RSA cryptosystem, where multiplying two large primes is trivial but recovering them from the product is computationally intractable. The emerging threat of Shor's quantum algorithm underscores the need for post-quantum cryptographic alternatives, ensuring that the interplay between prime number theory and information security will remain a vibrant area of research for decades to come.

Varsity Tutors • Discrete Math • Prime numbers and factorization concepts