diff --git a/X10D.Tests/src/Math/IsPrimeTests.cs b/X10D.Tests/src/Math/IsPrimeTests.cs
index 04ff694..f1255a6 100644
--- a/X10D.Tests/src/Math/IsPrimeTests.cs
+++ b/X10D.Tests/src/Math/IsPrimeTests.cs
@@ -24,8 +24,40 @@ public class IsPrimeTests
for (var index = 0; index < _primeNumbers.Count; index++)
{
int value = _primeNumbers[index];
- bool result = value.IsPrime();
- Assert.IsTrue(result, value.ToString("N0", CultureInfo.InvariantCulture));
+
+ Assert.IsTrue(value.IsPrime());
+ Assert.IsTrue(((long)value).IsPrime());
+
+ if (value is >= byte.MinValue and <= byte.MaxValue)
+ {
+ Assert.IsTrue(((byte)value).IsPrime());
+ }
+
+ if (value is >= short.MinValue and <= short.MaxValue)
+ {
+ Assert.IsTrue(((short)value).IsPrime());
+ }
+
+ if (value is >= byte.MinValue and <= byte.MaxValue)
+ {
+ Assert.IsTrue(((byte)value).IsPrime());
+ }
+
+ if (value is >= ushort.MinValue and <= ushort.MaxValue)
+ {
+ Assert.IsTrue(((ushort)value).IsPrime());
+ }
+
+ if (value is >= sbyte.MinValue and <= sbyte.MaxValue)
+ {
+ Assert.IsTrue(((sbyte)value).IsPrime());
+ }
+
+ if (value >= 0)
+ {
+ Assert.IsTrue(((uint)value).IsPrime());
+ Assert.IsTrue(((ulong)value).IsPrime());
+ }
}
}
@@ -34,8 +66,14 @@ public class IsPrimeTests
{
for (var value = short.MinValue; value < 0; value++)
{
- bool result = value.IsPrime();
- Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
+ Assert.IsFalse(value.IsPrime());
+ Assert.IsFalse(((int)value).IsPrime());
+ Assert.IsFalse(((long)value).IsPrime());
+
+ if (value is >= sbyte.MinValue and <= sbyte.MaxValue)
+ {
+ Assert.IsFalse(((sbyte)value).IsPrime());
+ }
}
}
@@ -44,8 +82,15 @@ public class IsPrimeTests
{
for (var value = 0; value < 2; value++)
{
- bool result = value.IsPrime();
- Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
+ Assert.IsFalse(value.IsPrime());
+ Assert.IsFalse(((byte)value).IsPrime());
+ Assert.IsFalse(((short)value).IsPrime());
+ Assert.IsFalse(((long)value).IsPrime());
+
+ Assert.IsFalse(((sbyte)value).IsPrime());
+ Assert.IsFalse(((ushort)value).IsPrime());
+ Assert.IsFalse(((uint)value).IsPrime());
+ Assert.IsFalse(((ulong)value).IsPrime());
}
}
@@ -55,9 +100,14 @@ public class IsPrimeTests
for (var value = 0; value < 7920; value++)
{
bool expected = _primeNumbers.Contains(value);
- bool actual = value.IsPrime();
- Assert.AreEqual(expected, actual, value.ToString("N0", CultureInfo.InvariantCulture));
+ Assert.AreEqual(expected, ((short)value).IsPrime());
+ Assert.AreEqual(expected, value.IsPrime());
+ Assert.AreEqual(expected, ((long)value).IsPrime());
+
+ Assert.AreEqual(expected, ((ushort)value).IsPrime());
+ Assert.AreEqual(expected, ((uint)value).IsPrime());
+ Assert.AreEqual(expected, ((ulong)value).IsPrime());
}
}
@@ -67,9 +117,20 @@ public class IsPrimeTests
for (byte value = 0; value < byte.MaxValue; value++)
{
bool expected = _primeNumbers.Contains(value);
- bool actual = value.IsPrime();
- Assert.AreEqual(expected, actual, value.ToString("N0", CultureInfo.InvariantCulture));
+ Assert.AreEqual(expected, value.IsPrime());
+ Assert.AreEqual(expected, ((short)value).IsPrime());
+ Assert.AreEqual(expected, ((int)value).IsPrime());
+ Assert.AreEqual(expected, ((long)value).IsPrime());
+
+ Assert.AreEqual(expected, ((ushort)value).IsPrime());
+ Assert.AreEqual(expected, ((uint)value).IsPrime());
+ Assert.AreEqual(expected, ((ulong)value).IsPrime());
+
+ if (value < sbyte.MaxValue)
+ {
+ Assert.AreEqual(expected, ((sbyte)value).IsPrime());
+ }
}
}
diff --git a/X10D/src/Math/UInt16Extensions.cs b/X10D/src/Math/UInt16Extensions.cs
index 05e8d54..0b815bf 100644
--- a/X10D/src/Math/UInt16Extensions.cs
+++ b/X10D/src/Math/UInt16Extensions.cs
@@ -1,4 +1,4 @@
-using System.Diagnostics.Contracts;
+using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D.Math;
@@ -62,6 +62,20 @@ public static class UInt16Extensions
return (value & 1) == 0;
}
+ ///
+ /// Returns a value indicating whether the current value is a prime number.
+ ///
+ /// The value whose primality to check.
+ ///
+ /// if is prime; otherwise, .
+ ///
+ [Pure]
+ [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
+ public static bool IsPrime(this ushort value)
+ {
+ return ((ulong)value).IsPrime();
+ }
+
///
/// Returns a value indicating whether the current value is not evenly divisible by 2.
///
diff --git a/X10D/src/Math/UInt32Extensions.cs b/X10D/src/Math/UInt32Extensions.cs
index 288cdad..c525b52 100644
--- a/X10D/src/Math/UInt32Extensions.cs
+++ b/X10D/src/Math/UInt32Extensions.cs
@@ -1,4 +1,4 @@
-using System.Diagnostics.Contracts;
+using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D.Math;
@@ -62,6 +62,20 @@ public static class UInt32Extensions
return (value & 1) == 0;
}
+ ///
+ /// Returns a value indicating whether the current value is a prime number.
+ ///
+ /// The value whose primality to check.
+ ///
+ /// if is prime; otherwise, .
+ ///
+ [Pure]
+ [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
+ public static bool IsPrime(this uint value)
+ {
+ return ((ulong)value).IsPrime();
+ }
+
///
/// Returns a value indicating whether the current value is not evenly divisible by 2.
///
diff --git a/X10D/src/Math/UInt64Extensions.cs b/X10D/src/Math/UInt64Extensions.cs
index 2753a02..3c58982 100644
--- a/X10D/src/Math/UInt64Extensions.cs
+++ b/X10D/src/Math/UInt64Extensions.cs
@@ -1,4 +1,4 @@
-using System.Diagnostics.Contracts;
+using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D.Math;
@@ -62,6 +62,45 @@ public static class UInt64Extensions
return (value & 1) == 0;
}
+ ///
+ /// Returns a value indicating whether the current value is a prime number.
+ ///
+ /// The value whose primality to check.
+ ///
+ /// if is prime; otherwise, .
+ ///
+ [Pure]
+ [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
+ public static bool IsPrime(this ulong value)
+ {
+ switch (value)
+ {
+ case < 2: return false;
+ case 2:
+ case 3: return true;
+ }
+
+ if (value % 2 == 0 || value % 3 == 0)
+ {
+ return false;
+ }
+
+ if ((value + 1) % 6 != 0 && (value - 1) % 6 != 0)
+ {
+ return false;
+ }
+
+ for (var iterator = 5UL; iterator * iterator <= value; iterator += 6)
+ {
+ if (value % iterator == 0 || value % (iterator + 2) == 0)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
///
/// Returns a value indicating whether the current value is not evenly divisible by 2.
///