C Program To Find Prime Numbers Between Given Range
Last Updated :
11 Sep, 2024
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. In this article, we will learn how to find all the prime numbers between the given range.
Example
Input: l = 10, r = 30
Output: 11 13 17 19
Explanation: The prime numbers between 10 and 20 are 11, 13, 17, and 19.
Input: l = 1, r = 10
Output: 2 3 5 7
Explanation: The prime numbers between 1 and 10 are 2, 3, 5, and 7.
Different Methods to Find Prime Numbers Between Given Range in C
There are mainly three different ways to find the prime numbers in the given range of natural numbers:
Simple Trial Division Method
The idea is to check the primality of each number in the given range [l, r] using Trial Division method. In this method, dividing it with every number from 2 to n - 1, where n is the given number. If the number is completely divisible with any number, it is not prime. Else it is prime.
C Program to Find the Prime Numbers Between Given Range Simple Trial Division
C
// C Program to Find Prime Numbers in a Given Range using
// Trial Division method
#include <stdbool.h>
#include <stdio.h>
bool isPrime(int n) {
// Checking primality by finding a complete division
// in the range 2 to n-1
if (n <= 1)
return false;
for (int i = 2; i < n; i++) {
if (n % i == 0)
return false;
}
return true;
}
void findPrimes(int l, int r) {
// Flag to check if any prime numbers are found
bool found = false;
for (int i = l; i <= r; i++) {
// Checking if the number is prime
if (isPrime(i)) {
printf("%d ", i);
found = true;
}
}
if (!found) {
printf(
"No prime numbers found in the given range.");
}
}
int main() {
int l = 10, r = 30;
// Finding and printing the prime between [l, r]
findPrimes(l, r);
return 0;
}
Time Complexity: O(r2), where r is the upper limit of the range.
Space Complexity: O(1)
Optimized Trial Division Method
We can optimize the above trial division method by using a mathematical fact that, "The smallest factor of a number greater than one cannot be greater than the square root of that number."
We will still check the primality of each number in the range one by one.
C Program to Find the Prime Numbers Between Given Range Using Simple Trial Division
C
// C Program to Find Prime Numbers in a Given Range using
// Simple Iterative Approach
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
bool isPrime(int n) {
// Checking 1 and 2 for primality
if (n <= 1)
return false;
if (n == 2)
return true;
// Checking for divisiblity by 2
if (n % 2 == 0)
return false;
// Checking for divisiblity from 3 to sqrt(n)
for (int i = 3; i * i <= n; i += 1)
if (n % i == 0)
return false;
return true;
}
void findPrimes(int l, int r) {
// Flag to check if any prime numbers are found
bool found = false;
for (int i = l; i <= r; i++) {
if (isPrime(i)) {
// Print the prime number
printf("%d ", i);
found = true;
}
}
if (!found) {
printf("No prime numbers found in the given "
"range.");
}
}
int main() {
int l = 10, r = 30;
// Finding and printing prime number between [l, r]
findPrimes(l, r);
return 0;
}
Time Complexity: O(r√r), where r is the upper limit of the range.
Space Complexity: O(1)
Sieve of Eratosthenes
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. The basic idea is to iteratively mark the multiples of each number starting from the lower limit l to upper limit r.. At the end, all the unmarked numbers in the rage will be prime numbers.
C Program to Find the Prime Numbers Between Given Range Using Sieve of Eratosthenes
C
// C Program to Find Prime Numbers in a Given Range using
// Sieve of Eratosthenes
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int* sieve(int n, int* size) {
// Allocate array to mark the multiples
bool* isPrime = (bool*)malloc((n + 1) * sizeof(bool));
// Initially, mark all the multiples as true
for (int i = 0; i <= n; i++) {
isPrime[i] = true;
}
// 0 and 1 are not prime numbers so mark
// them seperately
isPrime[0] = isPrime[1] = false;
// Sieve of Eratosthenes algorithm
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i) {
// Mark multiples of i as non-prime
isPrime[j] = false;
}
}
}
// Count the number of prime numbers
int count = 0;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
count++;
}
}
// Store all the prime numbers in seperate array
int* primes = (int*)malloc(count * sizeof(int));
int pi = 0;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
primes[pi++] = i;
}
}
// Set the size of the primes array
*size = count;
free(isPrime);
return primes;
}
void findPrimes(int l, int r) {
int size;
// Get all primes up to r
int* primes = sieve(r, &size);
// If there are no prime numbers in the range
if (size == 0) {
printf("There are no prime numbers between the "
"given range!\n");
return;
}
// Starting from the prime number just after the l
int start = 0;
while (primes[start] < l) {
start++;
}
// Printing all prime numbers in the range
for (int i = start; i < size; i++) {
printf("%d ", primes[i]);
}
free(primes);
}
int main() {
int l = 10, r = 30;
// Finding and printing prime numbers in range [l, r]
findPrimes(l, r);
return 0;
}
Time Complexity: O(r*log(log(r))), where r is the upper limit of the range.
Auxiliary Space: O(r)
Similar Reads
Find product of prime numbers between 1 to n Given a number n, we need to find the product of all prime numbers between 1 to n.Examples: Input: 5Output: 30Explanation: product of prime numbers between 1 to 5 is 2 * 3 * 5 = 30 Input : 7Output : 210 Approach: Basic brute-force method Steps: Initialize the product to be 1 and an empty list of pri
11 min read
Program to find all Sexy Primes in a range In mathematics, Sexy Primes are prime numbers that differ from each other by six. For example, the numbers 5 and 11 are both sexy primes, because they differ by 6. If p + 2 or p + 4 (where p is the lower prime) is also prime.They can be grouped as: Sexy prime pairs: It is of the form (p, p + 6), whe
7 min read
R program to find prime and composite numbers in an interval A natural number (1, 2, 3, 4, 5 and so on) is called a prime number if it is greater than 1 and cannot be written as the product of two smaller natural numbers. The numbers greater than 1 that are not prime are called composite numbers. A composite number is a positive integer that can be formed by
7 min read
Count of Double Prime numbers in a given range L to R Given two integers L and R, the task to find the number of Double Prime numbers in the range. A number N is called double prime when the count of prime numbers in the range 1 to N (excluding 1 and including N) is also prime. Examples: Input: L = 3, R = 10 Output: 4 Explanation: For 3, we have the ra
9 min read
Count Full Prime numbers in a given range Given two integers L and R, the task is to count the number of full prime numbers that are present in the given range. A number is said to be Full prime if the number itself is prime and all its digits are also prime. Examples: 53 is Full Prime because it is prime and all its digits (5 and 3) are al
8 min read