-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathPrimeTester.cs
35 lines (29 loc) · 937 Bytes
/
PrimeTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
namespace Advanced.Algorithms.Numerical;
/// <summary>
/// Tests for Prime in School method optimized.
/// </summary>
public class PrimeTester
{
/// <summary>
/// Check if given number is prime.
/// </summary>
public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number <= 3) return true;
//number can be divided by 2 or 3
if (number % 2 == 0 || number % 3 == 0) return false;
//skip six numbers in each step
//since we don't need to check for 3 even numbers
//and one number divisible by 3
//inside the loop
//check until square root of number
var sqrt = Math.Sqrt(number);
for (var i = 5; i <= sqrt; i = i + 6)
//check for two potential primes
if (number % i == 0 || number % (i + 2) == 0)
return false;
return true;
}
}