C Program to Display Prime Numbers Between Intervals
Last Updated :
12 May, 2023
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
Approach 1:
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.
Program:
C
#include <stdio.h>
int main()
{
int a, b, i, j, flag;
printf ( "Enter lower bound of the interval: " );
scanf ( "%d" , &a);
printf ( "Enter upper bound of the interval: " );
scanf ( "%d" , &b);
printf ( "Prime numbers between %d and %d are: " ,
a, b);
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)
printf ( "%d " , 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
Approach 2:
If a number ‘n’ is not divided by any number less than or equals to the square root of n then, it will not be divided by any other number greater than the square root of n. So, we only need to check up to the square root of n.
Below is the implementation of the above approach:
C
#include <stdbool.h>
#include <stdio.h>
bool isPrime( int n)
{
if (n == 1 || n == 0)
return false ;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
int main()
{
int a = 1;
int b = 10;
printf ( "Prime numbers between %d and %d are: " , a, b);
for ( int i = a; i <= b; i++) {
if (isPrime(i))
printf ( "%d " , i);
}
return 0;
}
|
Output
Prime numbers between 1 and 10 are: 2 3 5 7
Time Complexity: O(N*sqrt(N)), where max value of N is b.
Auxiliary Space: O(1), since no extra space has been taken.
Approach 3: (Using 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.
Steps:
- 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 implementation of the above approach:
C
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
void SieveOfEratosthenes( int srt, int n)
{
bool prime[n + 1];
memset (prime, true , sizeof (prime));
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; 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])
printf ( "%d " , p);
}
int main()
{
int srt = 1;
int end = 10;
SieveOfEratosthenes(srt, end);
return 0;
}
|
Complexity Analysis:
Time Complexity:
The time complexity of the Sieve of Eratosthenes algorithm is O(n*log(log(n))) as it iterates over all numbers from 2 to n and removes all the multiples of each prime number.
Auxiliary Space:
The space complexity of the algorithm is O(n), which is the size of the boolean array prime.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...