Segmented Sieve of Eratosthenes
Space-optimized variant of the Sieve of Eratosthenes that finds primes in segments, using only O(√n) space instead of O(n).
Visualization
Interactive visualization for Segmented Sieve of Eratosthenes
Sieve of Eratosthenes Visualization
Ancient algorithm to find all primes up to n in O(n log log n) time
Time Complexity: O(n log log n)
Space Complexity: O(n)
How it works:
- Mark all numbers from 2 to n as potential primes
- For each prime p starting from 2
- Mark all multiples of p (starting from p²) as composite
- Continue until p² > n
- Remaining marked numbers are primes
Interactive visualization with step-by-step execution
Implementation
1function sieveOfEratosthenes(n: number): number[] {
2 const isPrime = new Array(n + 1).fill(true);
3 isPrime[0] = isPrime[1] = false;
4
5 for (let p = 2; p * p <= n; p++) {
6 if (isPrime[p]) {
7 for (let i = p * p; i <= n; i += p) {
8 isPrime[i] = false;
9 }
10 }
11 }
12
13 const primes: number[] = [];
14 for (let i = 2; i <= n; i++) {
15 if (isPrime[i]) primes.push(i);
16 }
17
18 return primes;
19}
20
21// Optimized: only odd numbers
22function sieveOptimized(n: number): number[] {
23 if (n < 2) return [];
24 const primes = [2];
25 const isPrime = new Array(Math.floor((n - 1) / 2)).fill(true);
26
27 for (let i = 0; i < isPrime.length; i++) {
28 if (isPrime[i]) {
29 const p = 2 * i + 3;
30 primes.push(p);
31
32 for (let j = (p * p - 3) / 2; j < isPrime.length; j += p) {
33 isPrime[j] = false;
34 }
35 }
36 }
37
38 return primes;
39}Deep Dive
Theoretical Foundation
Mark multiples of each prime as composite. Start with 2, mark 4,6,8... Then 3, mark 6,9,12... Continue with next unmarked number. Unmarked numbers are prime. Optimization: only check up to √n, start marking from p².
Complexity
Time
O(n log log n)
O(n log log n)
O(n log log n)
Space
O(n)
Applications
Industry Use
RSA cryptography key generation
Hash table size optimization (prime sizes)
Number theory research and computation
Prime factorization preprocessing
Computational mathematics libraries
Blockchain proof-of-work algorithms
Random number generator seeding
Mathematical competition problems
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.