1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 04:05:40 +00:00

Add Int64.IsPrime

This commit is contained in:
Oliver Booth 2019-12-17 11:50:54 +00:00
parent b0e5073b39
commit d2208f2f6a
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0

View File

@ -204,7 +204,7 @@
return false; return false;
} }
long boundary = (long)Math.Floor(Math.Sqrt(number)); long boundary = (long) Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2) for (int i = 3; i <= boundary; i += 2)
{ {
if (number % i == 0) if (number % i == 0)
@ -216,6 +216,41 @@
return true; 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> /// <summary>
/// Gets an boolean value that represents this integer. /// Gets an boolean value that represents this integer.
/// </summary> /// </summary>