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. 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.
53 59 61 67 71 73 79 83 89 97
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count all prime numbers in a given range whose sum of digits is also prime
- Sum of all the prime numbers in a given range
- Sum of all prime divisors of all the numbers in range L-R
- Print prime numbers in a given range using C++ STL
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- K-Primes (Numbers with k prime factors) in a range
- Count numbers from range whose prime factors are only 2 and 3
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count of Double Prime numbers in a given range L to R
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Queries for the difference between the count of composite and prime numbers in a given range
- Find the highest occurring digit in prime numbers in a range
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.