For large n, general(or simple) Sieve of Eratosthenes may cause two problems.
- Space: The sieve array's size doesn't fit in memory.
- Time: A large sieve decrease spatial locality. Then the performance of a program become worse.
Comparison Complexity
The space complexity of basic Sieve of Eratosthenes: O(n)
The space complexity of Segmented Sieve of Eratosthenes(Size of Regular Sieve) : O(√n)
By Wikipedia link, Segmented Sieve of Eratosthenes works as follows.
The space complexity of basic Sieve of Eratosthenes: O(n)
The space complexity of Segmented Sieve of Eratosthenes(Size of Regular Sieve) : O(√n)
By Wikipedia link, Segmented Sieve of Eratosthenes works as follows.
- Divide the range 2 through n into segments of some size Δ ≤ √n.
- Find the primes up to Δ, using the "regular" sieve.
- For each Δ-sized block from √n + 1 to n, set up a Boolean array of size Δ. Eliminate the multiples of each prime p ≤ √n found in step 2.
Below is C implementation of 'Segmented Sieve of Eratosthenes'
No comments:
Post a Comment