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.

Examples:

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. We have discussed one implementation in Prime numbers in a given range using STL | Set 1

- Find primes in the range from 0 to end and store it in a vector
- Find the index of element less than start value using binary search. We use lower_bound() in STL.
- Erase elements from the beginning of the vector to that index. We use vector erase()

Viola! The vector contains primes ranging from start to end.

`// C++ program to print all primes ` `// in a range using Sieve of Eratosthenes ` `#include <algorithm> ` `#include <cmath> ` `#include <iostream> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `#define all(v) v.begin(), v.end() ` `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]) ` ` ` `ans.push_back(i); ` ` ` `return` `ans; ` `} ` ` ` `vector<ulli> sieveRange(ulli start, ulli end) ` `{ ` ` ` `// find primes from [0..end] range ` ` ` `vector<ulli> ans = sieve(end); ` ` ` ` ` `// Find index of first prime greater than or ` ` ` `// equal to start ` ` ` `// O(sqrt(n)loglog(n)) ` ` ` `int` `lower_bound_index = lower_bound(all(ans), start) - ` ` ` `ans.begin(); ` ` ` ` ` `// Remove all elements smaller than start. ` ` ` `// O(logn) ` ` ` `ans.erase(ans.begin(), ans.begin() + lower_bound_index); ` ` ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

53 59 61 67 71 73 79 83 89 97

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print prime numbers in a given range using C++ STL
- Count all prime numbers in a given range whose sum of digits is also prime
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Sum of all the prime numbers in a given range
- Queries for the difference between the count of composite and prime numbers in a given range
- Count of Double Prime numbers in a given range L to R
- C/C++ Program to find Prime Numbers between given range
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Square free semiprimes in a given range using C++ STL
- Count number of unique Triangles using STL | Set 1 (Using set)
- Prime numbers after prime P with sum S

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.