(Translated by https://www.hiragana.jp/)
Segmented Sieve - GeeksforGeeks
Open In App

Segmented Sieve

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n, print all primes smaller than n.

Input: N = 10
Output: 2, 3, 5, 7
Explanation : The output “2, 3, 5, 7” for input N = 10 represents the list of the prime numbers less than or equal to 10.

Input: N = 5
Output: 2, 3, 5
Explanation : The output “2, 3, 5” for input N = 5 represents the list of the prime numbers less than or equal to 5.

A Naive approach is to run a loop from 0 to n-1 and check each number for primeness. A Better Approach is to use Simple Sieve of Eratosthenes.

C++
C Java Python C# JavaScript

Problems with Simple Sieve:
The Sieve of Eratosthenes looks good, but consider the situation when n is large, the Simple Sieve faces the following issues.

  • An array of size ?(n) may not fit in memory
  • The simple Sieve is not cached friendly even for slightly bigger n. The algorithm traverses the array without locality of reference

Segmented Sieve
The idea of a segmented sieve is to divide the range [0..n-1] in different segments and compute primes in all segments one by one. This algorithm first uses Simple Sieve to find primes smaller than or equal to ?(n). Below are steps used in Segmented Sieve.

  1. Use Simple Sieve to find all primes up to the square root of ‘n’ and store these primes in an array “prime[]”. Store the found primes in an array ‘prime[]’.
  2. We need all primes in the range [0..n-1]. We divide this range into different segments such that the size of every segment is at-most ?n
  3. Do following for every segment [low..high] 
    • Create an array mark[high-low+1]. Here we need only O(x) space where x is a number of elements in a given range.
    • Iterate through all primes found in step 1. For every prime, mark its multiples in the given range [low..high].

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)

Below is the implementation of the above idea.

C++
Java Python C# JavaScript

Output
Primes smaller than 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

Time Complexity : O(n * ln(sqrt(n)))

Auxiliary Space: O(sqrt(n))

Note that time complexity (or a number of operations) by Segmented Sieve is the same as Simple Sieve. It has advantages for large 'n' as it has better locality of reference thus allowing better caching by the CPU and also requires less memory space.
 


Segmented Sieve
Visit Course explore course icon

Similar Reads