Solved Problem - Segmented Sieve
Understand how to implement the segmented sieve algorithm to efficiently find all prime numbers between given large integers. This lesson explains the problem constraints, why the standard sieve is inefficient, and guides you through the time and memory optimized approach for ranges up to 10^9 with difference up to 10^6.
We'll cover the following...
We'll cover the following...
Problem statement
Given two integers, and , print all primes between and (both inclusive).
Input format
The only line of input contains two integers and .
Sample
Input
100000000 100000100
Output
100000007 100000037 100000039 100000049 100000073 100000081
Sieve
Obviously, Sieve of Eratosthenes comes to mind. Now, two things don’t work right here:
- , we don’t have enough memory to declare an array of a billion integers (4 GB memory).
- Even if we could declare an array that big, the time complexity would be , which is obviously very slow.
Observation:
Segmented sieve
Since ...