
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Find Prime Numbers Within a Range
In this article, we are going to discuss how we can find prime numbers present within a given range using C#.
What are prime numbers?
Prime numbers are sets of natural numbers greater than 1 that have only two factors i.e. 1 and itself. They have no other positive divisors other than 1 and itself. Examples of prime numbers are 2, 3, 5, 7, etc.
Which is the Smallest prime number?
The smallest prime number is 2.
Is 1 a prime Number?
No, 1 is not a prime number.
Problem Description
We are given two numbers which are starting and ending values and we have to find prime numbers between these numbers. These numbers are the range in which we have to find the prime number.
Example
Input:
start = 900
end = 1000
Output:
907 911 919 929 937 941 947 953 967 971 977 983 991 997
Explanation: The prime numbers between 900 and 1000 are 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, and 997. These numbers have only two factors i.e. 1 and itself.
Input:
start = 30
end = 50
Output:
31 37 41 43 47
Explanation: The prime numbers between 30 and 50 are 31, 37, 41, 43, and 47. These numbers have only two factors i.e. 1 and itself.
Input:
start = 160
end = 190
Output:
163 167 173 179 181
Explanation: The prime numbers between 160 and 190 are 163, 167, 173, 179, and 181. These numbers have only two factors i.e. 1 and itself.
Find Prime Numbers Within a Range Using Brute Force Approach
In the brute force approach, we loop through all the numbers in the given range i.e. start and end. We check for each number if it is divisible by any number from 2 to its square root. If a number has no divisors other than 1 and itself, it is a prime number.
Steps for Implementation
- Implement a function to check if a number is prime using a loop and modulus operation.
- Create another function that takes a range (int start, int end) to loop through the range and call IsPrime for each number.
- Finally, print all numbers identified as prime within the specified range.
Implementation Code
using System; class Program { static bool IsPrime(int number) { if (number <= 1) return false; for (int i = 2; i * i <= number; i++) { if (number % i == 0) return false; } return true; } static void FindPrimesInRange(int start, int end) { Console.WriteLine($"Prime numbers between {start} and {end} are:"); for (int i = start; i <= end; i++) { if (IsPrime(i)) { Console.Write(i + " "); } } Console.WriteLine(); } static void Main() { int start = 160; int end = 190; FindPrimesInRange(start, end); } }
Output:
Prime numbers between 160 and 190 are:
163 167 173 179 181
Time Complexity: O((end?start)?n), where end and start are the range of numbers, and n is the average number of checks per number.
Space Complexity: O(1), constant space.
Find Prime Numbers Within a Range Using Optimized Approach
In the optimized approach, we loop through all the numbers given in the range i.e. starting and ending points. We start from 2 and for each number, we check divisors only up to its square root. We skip even numbers after 2 to reduce unnecessary checks. We finally print all prime numbers.
Steps for Implementation
- Create a function that handles edge cases (e.g., ?1, even numbers) and checks divisors from 3 to ?(number), skipping even numbers.
- Implement a function for the range (start and end) to loop through the given range and call IsPrimeOptimized for each number.
- Finally, print numbers identified as prime within the specified range.
Implementation Code
using System; class Program { static bool IsPrimeOptimized(int number) { if (number <= 1) return false; if (number == 2) return true; if (number % 2 == 0) return false; // Check divisors from 3 to sqrt(number), skipping even numbers for (int i = 3; i * i <= number; i += 2) { if (number % i == 0) return false; } return true; } static void FindPrimesInRangeOptimized(int start, int end) { Console.WriteLine($"Prime numbers between {start} and {end} are:"); for (int i = start; i <= end; i++) { if (IsPrimeOptimized(i)) { Console.Write(i + " "); } } Console.WriteLine(); } static void Main() { int start = 160; int end = 190; FindPrimesInRangeOptimized(start, end); } }
Output:
Prime numbers between 160 and 190 are:
163 167 173 179 181
Time Complexity: O((end?start)??n), as we are checking fewer divisors.
Space Complexity: O(1), constant space.