Time complexity of Sieve of Eratosthenes algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The Sieve of Eratosthenes is one of the most efficient algorithms for finding all prime numbers up to a specified integer. This ancient algorithm is attributed to the Greek mathematician Eratosthenes, and it has stood the test of time due to its simplicity and efficiency. Understanding its time complexity offers insight into its performance characteristics and suitability for different applications.
Understanding the Sieve of Eratosthenes
The Sieve of Eratosthenes operates by iteratively marking the multiples of each prime number starting from 2. The marked numbers are non-prime (composite), and those that remain unmarked are prime. Here’s how the algorithm works:
- Create a list of consecutive integers from 2 to
n. - Let
pequal 2, the smallest prime number. - Cross out all multiples of
p(except forpitself). - Find the smallest number larger than
pthat is not crossed out. Letpnow equal this new number. - Repeat steps 3 and 4 until
p^2 > n. - The remaining unmarked numbers are all prime.
Analyzing Time Complexity
To determine the algorithm's time complexity, we need to consider the operations at each step:
- Initialization: Creating a list from 2 to
ntakes time. - Sieving Process: For each prime number
p, the algorithm marks multiples ofpfrom ton$, skipping all previously marked multiples. This marking process is the most computationally intensive part.
Detailed Analysis
- Number of Operations: For a prime number
p, the total number of markings in a range up tonis approximately:2. Total Complexity: We consider the sum of markings for each prime number. The sieve effectively visits each number from 2 tona constant number of times due to the harmonic series approximation:### Space Complexity
The algorithm requires an array to keep track of prime status for each number, resulting in a space complexity of .
Key Considerations
- Memory Efficiency: While the Sieve of Eratosthenes is efficient in terms of time complexity, it requires significant memory, especially for large
n. Optimizations such as the "Segmented Sieve" can help mitigate this by reducing space usage. - Comparative Performance: Compared to trial division, which has a time complexity of , the Sieve is far superior for generating a list of primes.
- Limitations: For extremely large values of
n, alternative algorithms like the Sieve of Atkin or probabilistic tests may be preferred due to space constraints.
Example
To illustrate the sieving process, consider n = 30.
- Start with a list:
[2, 3, 4, 5, 6, ..., 30]. - Begin with
p = 2: Mark[4, 6, 8, ..., 30]. - Next
p = 3: Mark[9, 12, 15, ..., 30]. - Continue through primes up to , i.e., 5, marking multiples.
The remaining numbers, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29], are primes.
Summary Table
| Aspect | Details |
| Basic Algorithm Steps | Initialize, iterate, mark multiples |
| Initialization Cost | |
| Marking Cost | |
| Space Complexity | |
| Applications | Efficient prime generation Limited by memory constraints |
Conclusion
The Sieve of Eratosthenes remains a cornerstone algorithm for tasks involving prime number generation. Its clever use of iteration and marking delivers a time complexity of , making it a pragmatic choice for many computational tasks. However, as problems scale, considerations around memory and alternative methods become increasingly important. Understanding these nuances is crucial for effectively utilizing the Sieve of Eratosthenes in practice.
Related reading
- Time complexity of System.arraycopy...?
- Time complexity of the Ford-Fulkerson method in a flow network with unit capacity edges
- Time Complexity of the Kruskal Algorithm?
- Time Complexity Of This Code Snippet
- Time Complexity of two for loops
- Time complexity to generate all pairs in an array
- Time/Space Complexity of Depth First Search
- Tinyurl-style unique code potential algorithm to prevent collisions

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.