C++ Program To Find Prime Numbers Between Given Interval
A prime number is defined as a natural number greater than 1 and is divisible by only 1 and itself. In other words, the prime number is a positive integer greater than 1 that has exactly two factors, 1 and the number itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 . . .
Note:Â 1 is not either prime or composite. The remaining numbers, except for 1, are classified as prime and composite numbers.Â
Given two numbers a and b as interval range, the task is to find the prime numbers in between this interval.
Examples:Â
Input: a = 1, b = 10
Output: 2, 3, 5, 7
Input: a = 10, b = 20
Output: 11, 13, 17, 19
1. Using For Loop
In the below program, the range of numbers is taken as input and stored in the variables ‘a’ and ‘b’. Then using for-loop, the numbers between the interval of a and b are traversed. For each number in the for loop, it is checked if this number is prime or not. If found prime, print the number. Then the next number in the loop is checked, till all numbers are checked.
Below is the C++ program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, i, j, flag;
cout << "Enter lower bound of the interval: " ;
cin >> a;
cout << "Enter upper bound of the interval: " ;
cin >> b;
cout << "Prime numbers between " << a << " and " << b
<< " are: " ;
for (i = a; i <= b; i++) {
if (i == 1 || i == 0)
continue ;
flag = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break ;
}
}
if (flag == 1)
cout << i << " " ;
}
return 0;
}
|
Output
Enter lower bound of the interval: 1
Enter upper bound of the interval: 10
Prime numbers between 1 and 10 are: 2 3 5 7
The complexity of the above method
Time complexity: O((b-a)*(b/2)), a and b are the lower and upper limits.
Auxiliary space: O(1)
2. Optimized Solution
The idea is to use the fact that even numbers (except 2) are not primes.
Below is the C++ program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, i, j;
cout << "Enter lower bound of the interval: " ;
cin >> a;
cout << "Enter upper bound of the interval: " ;
cin >> b;
cout << "Prime numbers between " << a << " and " << b
<< " are: " ;
if (a <= 2) {
a = 2;
if (b >= 2) {
cout << a << " " ;
a++;
}
}
if (a % 2 == 0)
a++;
for (i = a; i <= b; i = i + 2) {
bool flag = 1;
for (j = 2; j * j <= i; ++j) {
if (i % j == 0) {
flag = 0;
break ;
}
}
if (flag == 1) {
if (i == 1)
continue ;
else
cout << i << " " ;
}
}
return 0;
}
|
Output:
Enter lower bound of the interval: 1
Enter upper bound of the interval: 10
Prime numbers between 1 and 10 are: 2 3 5 7
The complexity of the above method
Time complexity: O((b-a)*(sqrt(b))).
Auxiliary space: O(1).
3. Sieve of Eratosthenes Method
The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than N when N is smaller than 10 million or so.
Approach
- Create a boolean array prime[srt to n] and initialize all its entries as true.
- Mark prime[0] and prime[1] as false because they are not prime numbers.
- Starting from p = srt, for each prime number p less than or equal to the square root of n, mark all multiples of p greater than or equal to p*p as composite by setting prime[i] to false.
- Finally, print all prime numbers between srt and n.
Below is the C++ program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes( int srt, int n)
{
bool prime[n + 2 - srt];
memset (prime, true , sizeof (prime));
prime[0] = false ;
prime[1] = false ;
for ( int p = srt; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
for ( int p = srt; p <= n; p++)
if (prime[p])
cout << p << " " ;
}
int main()
{
int srt = 1;
int end = 10;
SieveOfEratosthenes(srt, end);
return 0;
}
|
The complexity of the above method
Time Complexity: O(n*log(log(n))) as it iterates over all numbers from 2 to n and removes all the multiples of each prime number. In this code, the algorithm is applied only to a range [srt, n], which reduces the time complexity to O((n-srt+1)*log(log(n))) for the range [srt, n]. The loop for marking the multiples of each prime number iterates from p*p to n, which takes O((n-srt+1)*log(log(n))) time. Therefore, the overall time complexity of this code is O((n-srt+1)*log(log(n))).
Auxiliary Space: O(n), which is the size of the boolean array prime. However, the size of the array is reduced to n+2-srt, which is the size of the array required for the given range [srt, n]. Therefore, the auxiliary space complexity of the code is O(n-srt+1).
Last Updated :
28 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...