Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Different Methods to find Prime Numbers in C#
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In C#, there are several methods to check if a number is prime, each with different approaches and efficiency levels.
What is a Prime Number?
A prime number is divisible only by 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers. The number 1 is not considered prime, and 2 is the only even prime number.
Using Basic Loop Method
This method counts the number of divisors. If exactly 2 divisors exist (1 and the number itself), it's prime −
using System;
class Demo {
public static void Main() {
int n = 7;
int divisorCount = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
divisorCount++;
}
}
if (divisorCount == 2) {
Console.WriteLine(n + " is a Prime Number");
} else {
Console.WriteLine(n + " is Not a Prime Number");
}
}
}
The output of the above code is −
7 is a Prime Number
Using Function with Optimized Loop
This approach uses a function and optimizes by checking divisors only up to n-1, stopping early when a divisor is found −
using System;
class Demo {
static void Main() {
int n = 17;
bool isPrime = IsPrimeNumber(n);
if (isPrime) {
Console.WriteLine(n + " is a Prime Number");
} else {
Console.WriteLine(n + " is Not a Prime Number");
}
}
private static bool IsPrimeNumber(int n) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i <= n - 1; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
The output of the above code is −
17 is a Prime Number
Using Square Root Optimization
The most efficient approach checks divisors only up to the square root of n, significantly reducing computation time −
using System;
class Demo {
static void Main() {
int[] numbers = {2, 4, 9, 13, 25, 29};
foreach (int num in numbers) {
bool isPrime = IsPrimeOptimized(num);
Console.WriteLine(num + " is " + (isPrime ? "Prime" : "Not Prime"));
}
}
private static bool IsPrimeOptimized(int n) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
int sqrt = (int)Math.Sqrt(n);
for (int i = 3; i <= sqrt; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
The output of the above code is −
2 is Prime 4 is Not Prime 9 is Not Prime 13 is Prime 25 is Not Prime 29 is Prime
Comparison of Methods
| Method | Time Complexity | Best For |
|---|---|---|
| Basic Loop (Count Divisors) | O(n) | Small numbers, learning |
| Function with Early Exit | O(n) | Medium numbers |
| Square Root Optimization | O(?n) | Large numbers, production code |
Conclusion
The square root optimization method is the most efficient for checking prime numbers, as it reduces the time complexity from O(n) to O(?n). For production applications dealing with large numbers, always use the optimized approach to minimize computation time.
