Skip to content
Related Articles

Related Articles

Improve Article

Print prime numbers in a given range using C++ STL

  • Difficulty Level : Hard
  • Last Updated : 18 Jul, 2017
Geek Week

Generate all prime numbers between two given numbers. The task is to print prime numbers in that range. The Sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n where n is smaller than 10 million or so.


Input : start = 50 end = 100
Output : 53 59 61 67 71 73 79 83 89 97

Input : start = 900 end = 1000
Output : 907 911 919 929 937 941 947 953 967 971 977 983 991 997

Idea is to use Sieve of Eratosthenes as a subroutine. Firstly, find primes in the range from 0 to start and store it in a vector. Similarly, find primes in the range from 0 to end and store in another vector. Now take the set difference of two vectors to obtain the required answer. Remove extra zeros if any in the vector.

// C++ STL program to print all primes 
// in a range using Sieve of Eratosthenes 
using namespace std;
typedef unsigned long long int ulli;
vector<ulli> sieve(ulli n)
    // Create a boolean vector "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    vector<bool> prime(n+1,true);
    prime[0] = false;
    prime[1] = false;
    int m = sqrt(n);
    for (ulli p=2; p<=m; p++)
        // If prime[p] is not changed, then it
        // is a prime 
        if (prime[p])
            // Update all multiples of p
            for (ulli i=p*2; i<=n; i += p)
            prime[i] = false;
    // push all the primes into the vector ans
    vector<ulli> ans;
    for (int i=0;i<n;i++)
        if (prime[i])
    return ans;
// Used to remove zeros from a vector using 
// library function remove_if()
bool isZero(ulli i)
    return i == 0;
vector<ulli> sieveRange(ulli start,ulli end)
    // find primes from [0..start] range
    vector<ulli> s1 = sieve(start);  
    // find primes from [0..end] range 
    vector<ulli> s2 = sieve(end);  
    vector<ulli> ans(end-start);
    // find set difference of two vectors and
    // push result in vector ans
    // O(2*(m+n)-1) 
    set_difference(s2.begin(), s2.end(), s1.begin(), 
                             s2.end(), ans.begin());
    // remove extra zeros if any. O(n)
    vector<ulli>::iterator itr =
    // resize it. // O(n)
    return ans;
// Driver Program to test above function
int main(void)
    ulli start = 50;
    ulli end = 100;
    vector<ulli> ans = sieveRange(start,end);
    for (auto i:ans)
        cout<<i<<' ';
    return 0;


53 59 61 67 71 73 79 83 89 97

This article is contributed by Varun Thakur. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

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 mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :