Skip to content

Commit e6bf302

Browse files
Add algorithm IsPrimeNumber (#562)
1 parent ce9915e commit e6bf302

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Algorithms.Numeric;
2+
3+
namespace Algorithms.Tests.Numeric;
4+
5+
public static class PrimeNumberTests
6+
{
7+
/// <summary>
8+
/// Tests the PrimeChecker.IsPrime method with various inputs (primes, composites, edge cases)
9+
/// to ensure the result is correct.
10+
/// </summary>
11+
[TestCase(-5, ExpectedResult = false)] // Negative number
12+
[TestCase(0, ExpectedResult = false)] // Zero
13+
[TestCase(1, ExpectedResult = false)] // One
14+
[TestCase(2, ExpectedResult = true)] // Smallest prime
15+
[TestCase(3, ExpectedResult = true)] // Prime
16+
[TestCase(4, ExpectedResult = false)] // Composite (2*2)
17+
[TestCase(7, ExpectedResult = true)] // Prime
18+
[TestCase(9, ExpectedResult = false)] // Composite (3*3)
19+
[TestCase(13, ExpectedResult = true)] // Prime
20+
[TestCase(15, ExpectedResult = false)] // Composite (3*5)
21+
[TestCase(25, ExpectedResult = false)] // Composite (5*5)
22+
[TestCase(29, ExpectedResult = true)] // Prime
23+
[TestCase(35, ExpectedResult = false)] // Composite (5*7)
24+
[TestCase(49, ExpectedResult = false)] // Composite (7*7)
25+
[TestCase(97, ExpectedResult = true)] // Larger prime
26+
[TestCase(100, ExpectedResult = false)] // Larger composite
27+
public static bool IsPrime_ResultIsCorrect(int number)
28+
{
29+
// Arrange is implicit here
30+
31+
// Act
32+
var result = PrimeChecker.IsPrime(number);
33+
34+
// Assert
35+
return result;
36+
}
37+
}

Algorithms/Numeric/PrimeChecker.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace Algorithms.Numeric;
2+
3+
/// <summary>
4+
/// A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
5+
/// </summary>
6+
public static class PrimeChecker
7+
{
8+
/// <summary>
9+
/// Checks if a number is a prime number or not using optimized trial division.
10+
/// This method avoids checking multiples of 2 and 3, optimizing the loop by checking
11+
/// divisors of the form 6k ± 1 up to the square root of the number.
12+
/// </summary>
13+
/// <param name="number">The integer number to check.</param>
14+
/// <returns>True if the number is prime; False otherwise.</returns>
15+
public static bool IsPrime(int number)
16+
{
17+
// Numbers less than or equal to 1 are not prime.
18+
if (number <= 1)
19+
{
20+
return false;
21+
}
22+
23+
// 2 and 3 are the first two prime numbers.
24+
if (number <= 3)
25+
{
26+
return true;
27+
}
28+
29+
// Check for divisibility by 2 and 3.
30+
if (number % 2 == 0 || number % 3 == 0)
31+
{
32+
return false;
33+
}
34+
35+
// Check for divisibility by numbers of the form 6k ± 1 up to sqrt(number).
36+
// The loop increments by 6 to skip known non-prime divisors.
37+
for (int i = 5; i <= number / i; i = i + 6)
38+
{
39+
// Check 6k - 1
40+
if (number % i == 0)
41+
{
42+
return false;
43+
}
44+
45+
// Check 6k + 1
46+
if (number % (i + 2) == 0)
47+
{
48+
return false;
49+
}
50+
}
51+
52+
// If no divisors are found, the number is prime.
53+
return true;
54+
}
55+
}

0 commit comments

Comments
 (0)