Prime Numbers: Brute Force and the Sieve of Eratosthenes
A prime number is an integer greater than 1 that has exactly two positive divisors: 1 and itself.
The sequence of primes begins:
2,3,5,7,11,13,17,19,23,29,31,…
An integer greater than 1 that has more than two divisors is called a composite number. The number 1 is neither prime nor composite.
1. Brute-Force Prime Generation
Given an integer n≥2, we wish to list all primes between 2 and n.
Definition-Based Brute Force
For every candidate integer x from 2 to n, test whether any divisor d from 2 to x−1 divides x evenly:
BruteForcePrimes(n): // Input: Integer n >= 2 // Output: List of all prime numbers <= n primes = empty list for x = 2 to n do: isPrime = true for d = 2 to x - 1 do: if x mod d == 0 then: isPrime = false break if isPrime then: primes.append(x) return primes
Efficiency Analysis:
In the worst case (when x is prime), the inner loop runs x−2 iterations.
Total comparisons:
x=2∑n(x−2)=2(n−2)(n−1)=Θ(n2)
Time Complexity:O(n2)
Space Complexity:O(1) auxiliary space.
Square-Root Optimization
If x=a×b, at least one factor must satisfy a≤x. If no divisor is found up to ⌊x⌋, x must be prime:
OptimizedBruteForcePrimes(n): primes = empty list for x = 2 to n do: isPrime = true for d = 2 to floor(sqrt(x)) do: if x mod d == 0 then: isPrime = false break if isPrime then: primes.append(x) return primes
Efficiency Analysis:
Inner loop runs at most x times.
Summing over all x≤n:
x=2∑nx≈∫1nx1/2dx=32n3/2=Θ(nn)
Time Complexity:O(nn) or O(n1.5).
2. Sieve of Eratosthenes
The Sieve of Eratosthenes is an ancient and substantially more efficient algorithm that systematically eliminates multiples of primes rather than repeatedly testing division.
Algorithm Strategy
Initialize a boolean array A[2..n] where every entry is initially true.
For p=2,3,…,⌊n⌋:
If A[p] is still true, then p is prime.
Cross out all multiples of p starting at p2:
p2,p2+p,p2+2p,⋯≤n(Note: Any multiple k×p with k<p has already been crossed out by smaller prime factors!)
Any number x remaining true at the end is prime.
SieveOfEratosthenes(n): // Input: Integer n >= 2 // Output: Array of all prime numbers <= n for p = 2 to n do A[p] = true for p = 2 to floor(sqrt(n)) do: if A[p] == true then: j = p * p while j <= n do: A[j] = false j = j + p primes = empty list for p = 2 to n do: if A[p] == true then: primes.append(p) return primes
Visual Trace: Sieve for n=25
⌊25⌋=5. We only need outer loop passes for p=2,3,5:
Number
After p=2 (eliminate ≥4)
After p=3 (eliminate ≥9)
After p=5 (eliminate ≥25)
Final Prime Status
2
Prime
Prime
Prime
Prime
3
Prime
Prime
Prime
Prime
4
Eliminated (2×2)
-
-
Composite
5
Prime
Prime
Prime
Prime
6
Eliminated (2×3)
-
-
Composite
7
Prime
Prime
Prime
Prime
9
Undecided
Eliminated (3×3)
-
Composite
11
Prime
Prime
Prime
Prime
13
Prime
Prime
Prime
Prime
15
Eliminated (2×…)
Eliminated (3×5)
-
Composite
25
Undecided
Undecided
Eliminated (5×5)
Composite
Time & Space Complexity
Time Complexity:
p≤n∑pn≈np≤n∑p1=Θ(nloglogn)
Because loglogn grows exceptionally slowly, this is virtually linear in practice.
Space Complexity:Θ(n) for the boolean table A[2..n].
Comparison Summary
Algorithm
Method
Worst-Case Time
Auxiliary Space
Key Advantage
Brute Force (Definition)
Test all d<x
Θ(n2)
Θ(1)
Simple to implement
Brute Force (Optimized)
Test all d≤x
Θ(nn)
Θ(1)
No memory overhead
Sieve of Eratosthenes
Eliminate multiples from p2
Θ(nloglogn)
Θ(n)
Vastly faster for ranges
Exercises
Exercise 1
In the Sieve of Eratosthenes, why does crossing out multiples of p start at p2 rather than 2p?
Show solution ↓Hide solution ↑
Solution
Any multiple of p smaller than p2 can be written as k×p where k<p.
Since k<p, k must have a prime divisor q≤k<p. Consequently, this number k×p was already crossed out when the algorithm processed the smaller prime q.
Starting at p2 eliminates redundant crossing out operations.
Exercise 2
Calculate the number of operations required by the Sieve of Eratosthenes vs. the Brute-Force definition algorithm for n=10,000.
Show solution ↓Hide solution ↑
Solution
Brute Force:≈2n2=2108=50,000,000 operations.
Sieve of Eratosthenes:≈nloge(logen)=10,000×ln(ln(10000))≈10,000×ln(9.21)≈10,000×2.22≈22,200 operations.
The Sieve is over 2,200 times faster for n=10,000.