Given two numbers L and R, the task is to find the prime numbers between L and R.
Input: L = 1, R = 10
Output: 2 3 5 7
Prime number between the 1 and 10 are 2, 3, 5, and 7
Input: L = 30, R = 40
Output: 31 37
Approach: The idea is to iterate from in the range [L, R] and check if any number in the given range is prime or not. If yes then print that number and check for the next number till we iterate all the numbers.
Below the implementation of the above approach:
2 3 5 7
Time Complexity: O((R-L)*N), where N is the number, and L and R are the given range.
Auxiliary Space: O(1)
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.
- Find the highest occurring digit in prime numbers in a range
- Program to find sum of prime numbers between 1 to n
- Program to find the LCM of two prime numbers
- Program to find Prime Numbers Between given Interval
- Count all prime numbers in a given range whose sum of digits is also prime
- Program to find count of numbers having odd number of divisors in given range
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- 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
- Print prime numbers in a given range using C++ STL
- Count of Double Prime numbers in a given range L to R
- Sum of numbers in a range [L, R] whose count of divisors is prime
- K-Primes (Numbers with k prime factors) in a range
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of natural numbers in range [L, R] which are relatively prime with N
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Queries for the difference between the count of composite and prime numbers in a given range
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.