A prime number is a natural number greater than 1 that has exactly two positive factors: 1 and the number itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, ....
- Numbers greater than 1 that have more than two factors are called composite numbers.
- The number 1 is neither prime nor composite.
Given two numbers a and b, the task is to find all prime numbers within the given interval.
Illustration
Input: a = 1, b = 10
Output: 2 3 5 7Input: a = 10, b = 20
Output: 11 13 17 19

Methods to Find Prime Numbers in a Given Interval
The following approaches can be used to find prime numbers in a given range:
Method 1: Using Trial Division
In this approach, every number in the given interval is checked for primality. A number is prime if no number other than 1 and itself divides it exactly.
Approach
- Traverse all numbers from a to b.
- Skip numbers smaller than 2.
- For each number, check whether it is divisible by any number from 2 to i - 1.
- Print the number if no divisor is found.
#include <iostream>
using namespace std;
bool isPrime(int n)
{
if (n < 2)
return false;
for (int i = 2; i < n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int a = 1, b = 10;
for (int i = a; i <= b; i++) {
if (isPrime(i))
cout << i << " ";
}
return 0;
}
Output
2 3 5 7
Method 2: Optimized Trial Division
The primality check can be optimized because if a number n has a divisor greater than its square root, it must also have a corresponding divisor smaller than its square root. Therefore, it is sufficient to check divisors up to √n.
Even numbers greater than 2 can also be skipped because they cannot be prime.
Approach
- Print 2 if it lies within the interval.
- Start checking from the first odd number greater than or equal to a.
- Check only odd numbers.
- For each number, test divisibility from 3 up to √n.
- Print the number if no divisor is found.
#include <iostream>
using namespace std;
bool isPrime(int n)
{
if (n < 2)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int a = 1, b = 10;
for (int i = a; i <= b; i++) {
if (isPrime(i))
cout << i << " ";
}
return 0;
}
Output
2 3 5 7
Method 3: Using Sieve of Eratosthenes
The Sieve of Eratosthenes is more efficient when we need to find all prime numbers up to a manageable upper limit. Instead of checking every number separately, it marks the multiples of each prime as composite.
Approach
- Create a boolean array from 0 to b and initialize all values as prime.
- Mark 0 and 1 as non-prime.
- Starting from 2, mark all multiples of each prime as non-prime.
- Continue until p * p <= b.
- Print the prime numbers that lie between a and b.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a = 1, b = 10;
vector<bool> isPrime(b + 1, true);
if (b >= 0)
isPrime[0] = false;
if (b >= 1)
isPrime[1] = false;
for (int p = 2; p * p <= b; p++) {
if (isPrime[p]) {
for (int i = p * p; i <= b; i += p)
isPrime[i] = false;
}
}
for (int i = max(a, 2); i <= b; i++) {
if (isPrime[i])
cout << i << " ";
}
return 0;
}
Output
2 3 5 7