mirror of https://github.com/oliverbooth/X10D
✨ Add Int64.IsPrime
This commit is contained in:
parent
b0e5073b39
commit
d2208f2f6a
|
@ -216,6 +216,41 @@
|
|||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="UInt64"/> is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
public static bool IsPrime(this ulong number)
|
||||
{
|
||||
if (number <= 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (number == 2)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (number % 2 == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ulong boundary = (ulong) Math.Floor(Math.Sqrt(number));
|
||||
for (uint i = 3; i <= boundary; i += 2)
|
||||
{
|
||||
if (number % i == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue