Sieve of Eratosthenes
Time Complexity
Algorithm Analysis
Prime Numbers
Computational Efficiency

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.

Practice algorithms

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:

  1. Create a list of consecutive integers from 2 to n.
  2. Let p equal 2, the smallest prime number.
  3. Cross out all multiples of p (except for p itself).
  4. Find the smallest number larger than p that is not crossed out. Let p now equal this new number.
  5. Repeat steps 3 and 4 until p^2 > n.
  6. 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 n takes O(n)O(n) time.
  • Sieving Process: For each prime number p, the algorithm marks multiples of p from p2p^2 to n$, skipping all previously marked multiples. This marking process is the most computationally intensive part.

Detailed Analysis

  1. Number of Operations: For a prime number p, the total number of markings in a range up to n is approximately:
    Number of multiples of p=np2p+1\text{Number of multiples of } p = \left\lfloor \frac{n - p^2}{p} \right\rfloor + 12. Total Complexity: We consider the sum of markings for each prime number. The sieve effectively visits each number from 2 to n a constant number of times due to the harmonic series approximation:
    O(n(12+13+15++1largest prime))=O(nloglogn)O\left(n \left(\frac{1}{2} + \frac{1}{3} + \frac{1}{5} + \cdots + \frac{1}{\text{largest prime}}\right)\right) = O(n \log \log n)### Space Complexity

The algorithm requires an array to keep track of prime status for each number, resulting in a space complexity of O(n)O(n).

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 O(nn)O(n\sqrt{n}), 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 30\sqrt{30}, i.e., 5, marking multiples.

The remaining numbers, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29], are primes.

Summary Table

AspectDetails
Basic Algorithm StepsInitialize, iterate, mark multiples
Initialization CostO(n)O(n)
Marking CostO(nloglogn)O(n \log \log n)
Space ComplexityO(n)O(n)
ApplicationsEfficient 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 O(nloglogn)O(n \log \log n), 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.