Given a range [low, high], print all primes in this range? For example, if the given range is [10, 20], then output is 11, 13, 17, 19.
A Naive approach is to run a loop from low to high and check each number for primeness.
A Better Approach is to precalculate primes up to the maximum limit using Sieve of Eratosthenes, then print all prime numbers in range.
The above approach looks good, but consider the input range [50000, 55000]. the above Sieve approach would precalculate primes from 2 to 50100. This causes a waste of memory as well as time. Below is the Segmented Sieve based approach.
Segmented Sieve (Background)
Below are basic steps to get an idea of how Segmented Sieve works
- Use Simple Sieve to find all primes up to a predefined limit (square root of ‘high’ is used in below code) and store these primes in an array “prime”. Basically we call Simple Sieve for a limit and we not only find prime numbers, but also puts them in a separate array prime.
- Create an array mark[high-low+1]. Here we need only O(n) space where n is number of elements in given range.
- Iterate through all primes found in step 1. For every prime, mark its multiples in given range [low..high].
So unlike simple sieve, we don’t check for all multiples of every number smaller than square root of high, we only check for multiples of primes found in step 1. And we don’t need O(high) space, we need O(sqrt(high) + n) space.
Below is the implementation of above idea.
11 13 15 17 19
Segmented Sieve (What if ‘high’ value of range is too high and range is also big)
Consider a situation where given high value is so high that neither sqrt(high) nor O(high-low+1) can fit in memory. How to find prims in range. For this situation, we run step 1 (Simple Sieve) only for a limit that can fit in memory. Then we divide given range in different segments. For every segment, we run step 2 and 3 considering low and high as end points of current segment. We add primes of current segment to prime before running the next segment.
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Segmented Sieve
- Longest sub-array of Prime Numbers using Segmented Sieve
- Sum of all Primes in a given range using Sieve of Eratosthenes
- Sieve of Sundaram to print all primes smaller than n
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Sieve of Eratosthenes
- Sieve of Atkin
- Sieve of Eratosthenes in 0(n) time complexity
- Prime Factorization using Sieve O(log n) for multiple queries
- Number of unmarked integers in a special sieve
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- K-Primes (Numbers with k prime factors) in a range
- Minimum difference between any two primes from the given range
- Minimum window size containing atleast P primes in every window of given range
- Count of primes in a given range that can be expressed as sum of perfect squares
- Print all safe primes below N
- Print all multiplicative primes <= N
- Palindromic Primes