Display Prime Numbers Between Two Intervals in C++



A prime number is a number greater than 1 that has no divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. In this article, we'll show you how to write a C++ program to display prime numbers between two intervals.

Example

For example, given the interval from 0 to 50, we want to print all the prime numbers within this interval.

Input: Starting interval: 0 Ending interval: 50 Output: Prime numbers in the interval [0, 50] are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Approaches to Display Prime Numbers Between Two Intervals

In C++, we can find prime numbers between two intervals using the following approaches:

Using Naive Approach

In this approach, we check each number in the interval to see if it has any divisors other than 1 and itself. If it does, it's not a prime. If it doesn't, we consider it prime.

Example

Here's a complete C++ program where we take two numbers as the interval limits and check each number one by one. If a number has no divisors other than 1 and itself, we print it as a prime number.

Open Compiler
#include <iostream> using namespace std; /// Function to check if a number is prime bool isPrime(int num) { if (num <= 1) return false; // 0 and 1 are not prime for (int i = 2; i < num; ++i) { // Check from 2 to num-1 if (num % i == 0) return false; // If divisible, not prime } return true; // Otherwise, it's prime } int main() { int lower = 0; int upper = 50; cout << "Starting interval: " << lower << endl; cout << "Ending interval: " << upper << endl<< endl; cout << "Prime numbers in the interval [" << lower << ", " << upper << "] are: "<<endl; for (int i = lower; i <= upper; ++i) { if (isPrime(i)) { cout << i << " "; } } return 0; }

The output below displays all prime numbers between 0 and 50 using a simple loop(naive approach).

Starting interval: 0
Ending interval: 50

Prime numbers in the interval [0, 50] are: 
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Time Complexity: O(n^2), because for each number in the range, we check all numbers less than it.

Space Complexity: O(1) because we use no extra space.

Using Square Root

In this approach, instead of checking all numbers from 2 to n-1, we check only up to sqrt(n). This is because if a number has any divisor larger than sqrt(n), then the smaller divisor must have already divided it. This significantly reduces the number of checks and makes the process more efficient.

Example

Below is a complete C++ program where, for every number in the given interval, we check if it is prime number by only looping up to the square root of that number.

Open Compiler
#include <iostream> #include <cmath> // for sqrt() function using namespace std; // Function to check if a number is prime using square root method bool isPrime(int num) { if (num <= 1) return false; // 0 and 1 are not prime for (int i = 2; i <= sqrt(num); ++i) { // Loop up to square root of num if (num % i == 0) return false; // If divisible by i, it's not prime } return true; // Otherwise, it's prime } int main() { int lower = 0; int upper = 50; cout << "Starting interval: " << lower << endl; cout << "Ending interval: " << upper << endl<<endl; cout << "Prime numbers in the interval [" << lower << ", " << upper << "] are: "<<endl; for (int i = lower; i <= upper; ++i) { if (isPrime(i)) { cout << i << " "; // Output prime numbers only } } return 0; }

Below is the output that shows the prime numbers between the given interval:

Starting interval: 0
Ending interval: 50

Prime numbers in the interval [0, 50] are: 
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 

Time Complexity: O(m * sqrt(n)), where m is the number of numbers in the interval and n is the largest number in the interval.

Space Complexity: O(1), as no additional space is used.

Using Sieve of Eratosthenes

In this approach, we prepare a list(array) of numbers up to the upper limit and mark all as prime. Then we remove non-primes by marking all multiples of each prime as false (non-prime). Finally, we print only the primes that lie within our interval.

Example

In this example, we take the lower and upper bounds to apply the sieve algorithm to mark primes and then print all prime numbers within the given interval.

Open Compiler
#include <iostream> #include <vector> using namespace std; // Function to implement Sieve of Eratosthenes void sieveOfEratosthenes(int upper, vector<bool>& sieve) { sieve[0] = sieve[1] = false; // 0 and 1 are not prime numbers for (int i = 2; i * i <= upper; ++i) { if (sieve[i]) { // Mark multiples of i as non-prime for (int j = i * i; j <= upper; j += i) { sieve[j] = false; } } } } int main() { int lower = 0, upper = 50; cout << "Starting interval: " << lower << endl; // Initialize a sieve to track prime numbers vector<bool> sieve(upper + 1, true); sieveOfEratosthenes(upper, sieve); cout << "Ending interval: " << upper << endl << endl; cout << "Prime numbers in the interval [" << lower << ", " << upper << "] are: "<<endl; for (int i = lower; i <= upper; ++i) { if (sieve[i]) { cout << i << " "; // Print the prime numbers } } return 0; }

The output below shows the prime numbers found in the interval[0, 50] using the Sieve of Eratosthenes method:

Starting interval: 0
Ending interval: 50

Prime numbers in the interval [0, 50] are: 
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 

Time Complexity: O(n log log n) because we only mark each number as non-prime once, and we skip numbers that are already marked.

Space Complexity: O(n) because we use a boolean array of size(n+1) to store prime status for each number.

Conclusion

In this article, we showed how to find prime numbers between two intervals in C++ using different approaches. We covered methods like the naive approach, the square root method, and the Sieve of Eratosthenes. The naive and square root methods are good for smaller ranges, while the Sieve of Eratosthenes works better for larger ranges.

Updated on: 2025-05-13T18:46:48+05:30

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements