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 upto 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 Segmeented Sieve based approach.
Segmented Sieve (Background)
Below are basic steps to get idea how Segmented Sieve works
- Use Simple Sieve to find all primes upto 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 C++ implementation of above idea.
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
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
- Write a program to print all permutations of a given string
- Print all combinations of balanced parentheses
- Print all combinations of points that can compose a given number
- Print all sequences of given length
- Sieve of Eratosthenes
- Print all permutations in sorted (lexicographic) order
- Efficient program to print all prime factors of a given number
- Segment Tree | Set 1 (Sum of given range)
- Print all possible combinations of r elements in a given array of size n
- Print all possible words from phone digits
- Print squares of first n natural numbers without using *, / and -
- Print all increasing sequences of length k from first n natural numbers
- Print all distinct permutations of a given string with duplicates
- Count factorial numbers in a given range
- Print all non-increasing sequences of sum equal to a given number x
Improved By : RajatSinghal