Segmented & Incremental
Sieve
Both of these algorithms are efficient for finding prime
numbers in certain ranges. The Segmented Sieve is
particularly useful for finding primes in large ranges,
while the Incremental Sieve is useful for finding primes
starting from a given number.
A segmented sieve, also known as a segmented version of the Sieve of
Eratosthenes, is a way to find all prime numbers within a range of numbers,
rather than all prime numbers up to a certain number.
It is an efficient algorithm for generating prime numbers within a specific range.
The basic idea behind the segmented sieve is to divide the range of numbers into
segments and apply the Sieve of Eratosthenes to each segment.
In Simple Sieve, we needed O(n) space which may not be feasible for large n.
Here we need O(√n) space and we process smaller ranges at a time (locality of
reference)
ADVANTAGE
Segmented sieve is more memory efficient than the traditional sieve, as it only
needs to store the primes within a specific range, rather than all primes up to a
certain number.
This makes it well-suited for problems where large ranges of numbers need to be
searched for primes.
It’s worth noting that a Segmented sieve is faster than the traditional sieve when
the range of numbers is large.
It's also useful for finding primes in large intervals as it takes less memory space.
The algorithm works as follows:
The Segmented Sieve algorithm is used to find all prime numbers
within a given range `[L, R]`.
where `L` and `R` are two non-negative integers such that `L <= R`.
The basic idea behind this algorithm is to use the Sieve of
Eratosthenes method to find all prime numbers up to the square root
of `R`,
Then use these primes to eliminate all composite numbers in the
range `[L, R]`.
public class Segmented {
//step 3 Print all prime numbers
static void SegSieve(int l,int h)
for (int i = l; i <= h; i++)
{
{
//step1
if (prime[i] == false)
boolean prime[] = new boolean[h + 1];
System.out.print(i + " ");
//step 2
}
for (int p = 2; p * p <= h; p++) {
}
int sm=(l/p)*p;
public static void main(String[] args)
if (sm <l)
{
{
SegSieve(10, 30);
sm=sm+p;
}
}
}
for (int i = sm; i <= h; i += p)
prime[i] = true;
}
Incremental Sieve
Introduction
• The standard Sieve of Eratosthenes requires that you specify an
upper bound before you start
• But often, programs that use prime numbers don’t know the
upper bound in advance or don’t want to pre-compute and store
all of the primes up to their bound.
• In such cases, the Incremental Sieve algorithm can be used
The Incremental Sieve algorithm, on the other hand, is
used to find all prime numbers starting from a given
number `N`.
The basic idea behind this algorithm is to use the Sieve
of Eratosthenes method to find all prime numbers up to a
certain limit, say `L`,
Then use these primes to check if `N + i` is a prime
number for `i = 0, 1, 2, ...`.
Interview questions
What is the segmented sieve?
The segmented sieve is an algorithm that generates prime
numbers up to a certain limit, by dividing the range into smaller
segments and sieving each segment individually.
It is an efficient way to generate primes for a large range without
requiring a large amount of memory.
How does the segmented sieve work?
The segmented sieve works by first generating all the
primes up to the square root of the limit using a simple
sieve algorithm. Then, the range is divided into segments
of a certain size, and each segment is sieved using the
primes generated in the first step. Any remaining numbers
in the segment that are not marked as composite are prime
numbers.
What are the advantages of using the segmented
sieve over the simple sieve algorithm?
The main advantage of the segmented sieve over the simple
sieve algorithm is that it uses much less memory. The simple
sieve algorithm requires a large amount of memory to store all
the numbers in the range being sieved, while the segmented
sieve only requires memory to store the primes up to the square
root of the limit and the current segment being sieved.
What is the time complexity of segmented sieve?
The time complexity of the Segmented Sieve algorithm is O(n),
where n is the total number of elements present inside the given
range. The space complexity of the Segmented Sieve algorithm
is the same as the time complexity, i.e., O(n).
THANK YOU