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
1 changed files with 36 additions and 1 deletions

View File

@ -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>