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.
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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count all prime numbers in a given range whose sum of digits is also prime
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array
- Sum of all the prime numbers in a given range
- Prime numbers in a given range using STL | Set 2
- Sum of all prime divisors of all the numbers in range L-R
- Count of Double Prime numbers in a given range L to R
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count numbers from range whose prime factors are only 2 and 3
- Count of natural numbers in range [L, R] which are relatively prime with N
- K-Primes (Numbers with k prime factors) in a range
- C/C++ Program to find Prime Numbers between given range
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Print all prime numbers less than or equal to N
- Program to print first N Prime numbers
- Find the highest occurring digit in prime numbers in a range
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count numbers in a range having GCD of powers of prime factors equal to 1