Segmented Sieve of Eratosthenes?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Segmented Sieve of Eratosthenes is an optimization of the classic Sieve of Eratosthenes for finding all prime numbers within a specified range, particularly when the range is large and/or not starting from 1. This powerful algorithm reduces memory usage and improves efficiency by processing smaller segments of the range at a time. Understanding this algorithm requires an appreciation of its implementation and underlying mathematics.
The Rationale
When tasked with finding prime numbers within a range `[L, R]`, especially when `R` is very large, the classical sieve becomes inefficient due to its need to store prime number information for all numbers up to `R`. Instead, a segmented approach breaks the single large task into manageable pieces, providing significant improvements in both speed and memory consumption.
How it Works
The segmented sieve works in two main phases:
- Initial Sieve Precomputation: • Compute all primes up to using the classic Sieve of Eratosthenes. These primes will be used to mark non-prime numbers in each segment.
- Segmented Sieving: • Process the range `[L, R]` in smaller segments, typically of size . • For each segment, use the primes obtained from the initial sieve to mark non-primes. • Store the primes found in these segments.
Example
Suppose we wish to find all prime numbers in the range `[10, 50]`:
- Precomputation: • Calculate primes up to , that is, primes up to 7: `{2, 3, 5, 7}`.
- Segmented Sieving: • Segment 1: [10, 20] • Using the primes `{2, 3, 5, 7}`, mark multiples of each prime starting from their minimum multiple within the range. • Result: Primes in this segment are `{11, 13, 17, 19}`. • Segment 2: [21, 30] • Repeat the process: Primes are `{23, 29}`. • Segment 3: [31, 40] • Primes are `{31, 37}`. • Segment 4: [41, 50] • Primes are `{41, 43, 47}`.
The complete set of primes within `[10, 50]` is `{11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}`.
Implementation Details
Key considerations in implementing the Segmented Sieve of Eratosthenes:
• Efficient Memory Usage: By only storing prime numbers for segments of size , memory usage is tightly controlled. • Optimization: Avoid redundant calculations by marking multiples intelligently. • Edge Handling: Carefully handle the start and end of segments, particularly when `L` is not a multiple of the smallest prime.
Potential Optimizations
• Finding First Multiple Efficiently: Calculate the first multiple of each prime in the current segment without iterating from the start. • Bitwise Arrays: Use bit arrays instead of boolean arrays to reduce memory usage further.
Summary Table
| Feature | Description |
| Initial Phase | Compute primes up to with classic sieve |
| Segment Size | Typically |
| Used Primes | Primes up to are used for sieving |
| Memory Usage | Reduced with segmented approach |
| Complexity | for initial sieve; for segmented sieve |
| Scalability | Efficient for large ranges by avoiding direct large-range searches |
Benefits of Using Segmented Sieve
• Scalability: Handles large ranges efficiently. • Resource Efficiency: Reduces both time and space complexity. • Practical Application: Useful in cryptographic applications, numerical analysis, and computer science problems requiring large prime calculations.
Conclusion
The Segmented Sieve of Eratosthenes represents a sophisticated technique in computational number theory, allowing efficient prime generation over large ranges. Its practical utility and elegance make it a critical tool in both theoretical research and applied computing. Adopting this method can substantially enhance performance in applications where prime numbers are pivotal.
Related reading
- Select 50 items from list at random
- Select an element from a stream with uniform distributed probability
- Select combination of elements from array whose sum is smallest possible positive number
- Select k random elements from a list whose elements have weights
- Select all columns except one in MySQL?
- Select last N rows from MySQL
- Set of List of sets Cartesian products from graph corresponding to set of lists
- Set S of n numbers - have a subset with the probability of each element of S occuring in it equal

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.