diff --git a/X10D.Tests/src/Math/ByteTests.cs b/X10D.Tests/src/Math/ByteTests.cs index f955682..854d4b9 100644 --- a/X10D.Tests/src/Math/ByteTests.cs +++ b/X10D.Tests/src/Math/ByteTests.cs @@ -72,6 +72,56 @@ public class ByteTests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const byte value1 = 2; + const byte value2 = 3; + const byte expected = 6; + + byte result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const byte value1 = 0; + const byte value2 = 10; + const byte expected = 0; + + byte result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const byte value1 = 1; + const byte value2 = 10; + const byte expected = 10; + + byte result1 = value1.LowestCommonMultiple(value2); + byte result2 = value2.LowestCommonMultiple(value1); + + Assert.AreEqual(expected, result1); + Assert.AreEqual(expected, result2); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const byte value1 = 5; + const byte value2 = 5; + const byte expected = 5; + + byte result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D.Tests/src/Math/Int16Tests.cs b/X10D.Tests/src/Math/Int16Tests.cs index 3c78183..f2f8a31 100644 --- a/X10D.Tests/src/Math/Int16Tests.cs +++ b/X10D.Tests/src/Math/Int16Tests.cs @@ -72,6 +72,68 @@ public class Int16Tests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const short value1 = 2; + const short value2 = 3; + const short expected = 6; + + short result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const short value1 = 0; + const short value2 = 10; + const short expected = 0; + + short result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const short value1 = 1; + const short value2 = 10; + const short expected = 10; + + short result1 = value1.LowestCommonMultiple(value2); + short result2 = value2.LowestCommonMultiple(value1); + + Assert.AreEqual(expected, result1); + Assert.AreEqual(expected, result2); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const short value1 = 5; + const short value2 = 5; + const short expected = 5; + + short result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithNegativeValues() + { + const short value1 = -2; + const short value2 = 3; + const short expected = -6; + + short result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D.Tests/src/Math/Int32Tests.cs b/X10D.Tests/src/Math/Int32Tests.cs index c58f71a..ef8e45b 100644 --- a/X10D.Tests/src/Math/Int32Tests.cs +++ b/X10D.Tests/src/Math/Int32Tests.cs @@ -72,6 +72,68 @@ public class Int32Tests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const int value1 = 2; + const int value2 = 3; + const int expected = 6; + + int result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const int value1 = 0; + const int value2 = 10; + const int expected = 0; + + int result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const int value1 = 1; + const int value2 = 10; + const int expected = 10; + + int result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const int value1 = 5; + const int value2 = 5; + const int expected = 5; + + int result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithNegativeValues() + { + const int value1 = -2; + const int value2 = 3; + const int expected = -6; + + int result1 = value1.LowestCommonMultiple(value2); + int result2 = value2.LowestCommonMultiple(value1); + + Assert.AreEqual(expected, result1); + Assert.AreEqual(expected, result2); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D.Tests/src/Math/Int64Tests.cs b/X10D.Tests/src/Math/Int64Tests.cs index ee19d8f..752d9d6 100644 --- a/X10D.Tests/src/Math/Int64Tests.cs +++ b/X10D.Tests/src/Math/Int64Tests.cs @@ -72,6 +72,68 @@ public class Int64Tests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const long value1 = 2; + const long value2 = 3; + const long expected = 6; + + long result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const long value1 = 0; + const long value2 = 10; + const long expected = 0; + + long result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const long value1 = 1; + const long value2 = 10; + const long expected = 10; + + long result1 = value1.LowestCommonMultiple(value2); + long result2 = value2.LowestCommonMultiple(value1); + + Assert.AreEqual(expected, result1); + Assert.AreEqual(expected, result2); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const long value1 = 5; + const long value2 = 5; + const long expected = 5; + + long result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithNegativeValues() + { + const long value1 = -2; + const long value2 = 3; + const long expected = -6; + + long result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D.Tests/src/Math/SByteTests.cs b/X10D.Tests/src/Math/SByteTests.cs index 116c1d8..2664a63 100644 --- a/X10D.Tests/src/Math/SByteTests.cs +++ b/X10D.Tests/src/Math/SByteTests.cs @@ -73,6 +73,68 @@ public class SByteTests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const sbyte value1 = 2; + const sbyte value2 = 3; + const sbyte expected = 6; + + sbyte result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const sbyte value1 = 0; + const sbyte value2 = 10; + const sbyte expected = 0; + + sbyte result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const sbyte value1 = 1; + const sbyte value2 = 10; + const sbyte expected = 10; + + sbyte result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const sbyte value1 = 5; + const sbyte value2 = 5; + const sbyte expected = 5; + + sbyte result1 = value1.LowestCommonMultiple(value2); + sbyte result2 = value2.LowestCommonMultiple(value1); + + Assert.AreEqual(expected, result1); + Assert.AreEqual(expected, result2); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithNegativeValues() + { + const sbyte value1 = -2; + const sbyte value2 = 3; + const sbyte expected = -6; + + sbyte result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D.Tests/src/Math/UInt16Tests.cs b/X10D.Tests/src/Math/UInt16Tests.cs index dd98c88..4d74154 100644 --- a/X10D.Tests/src/Math/UInt16Tests.cs +++ b/X10D.Tests/src/Math/UInt16Tests.cs @@ -73,6 +73,56 @@ public class UInt16Tests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const ushort value1 = 2; + const ushort value2 = 3; + const ushort expected = 6; + + ushort result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const ushort value1 = 0; + const ushort value2 = 10; + const ushort expected = 0; + + ushort result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const ushort value1 = 1; + const ushort value2 = 10; + const ushort expected = 10; + + ushort result1 = value1.LowestCommonMultiple(value2); + ushort result2 = value2.LowestCommonMultiple(value1); + + Assert.AreEqual(expected, result1); + Assert.AreEqual(expected, result2); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const ushort value1 = 5; + const ushort value2 = 5; + const ushort expected = 5; + + ushort result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D.Tests/src/Math/UInt32Tests.cs b/X10D.Tests/src/Math/UInt32Tests.cs index 2a7cc54..1573b9b 100644 --- a/X10D.Tests/src/Math/UInt32Tests.cs +++ b/X10D.Tests/src/Math/UInt32Tests.cs @@ -73,6 +73,56 @@ public class UInt32Tests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const uint value1 = 2; + const uint value2 = 3; + const uint expected = 6; + + uint result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const uint value1 = 0; + const uint value2 = 10; + const uint expected = 0; + + uint result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const uint value1 = 1; + const uint value2 = 10; + const uint expected = 10; + + uint result1 = value1.LowestCommonMultiple(value2); + uint result2 = value2.LowestCommonMultiple(value1); + + Assert.AreEqual(expected, result1); + Assert.AreEqual(expected, result2); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const uint value1 = 5; + const uint value2 = 5; + const uint expected = 5; + + uint result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D.Tests/src/Math/UInt64Tests.cs b/X10D.Tests/src/Math/UInt64Tests.cs index b0e5e54..35e8d4c 100644 --- a/X10D.Tests/src/Math/UInt64Tests.cs +++ b/X10D.Tests/src/Math/UInt64Tests.cs @@ -1,4 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; using X10D.Math; namespace X10D.Tests.Math; @@ -77,6 +77,54 @@ public class UInt64Tests Assert.IsFalse(two.IsOdd()); } + [TestMethod] + public void LowestCommonMultiple_ShouldReturnCorrectValue_WhenCalledWithValidInput() + { + const ulong value1 = 2; + const ulong value2 = 3; + const ulong expected = 6; + + ulong result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnZero_WhenCalledWithZero() + { + const ulong value1 = 0; + const ulong value2 = 10; + const ulong expected = 0; + + ulong result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnGreaterValue_WhenCalledWithOne() + { + const ulong value1 = 1; + const ulong value2 = 10; + const ulong expected = 10; + + ulong result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + + [TestMethod] + public void LowestCommonMultiple_ShouldReturnOtherValue_WhenCalledWithSameValue() + { + const ulong value1 = 5; + const ulong value2 = 5; + const ulong expected = 5; + + ulong result = value1.LowestCommonMultiple(value2); + + Assert.AreEqual(expected, result); + } + [TestMethod] public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() { diff --git a/X10D/src/Math/ByteExtensions.cs b/X10D/src/Math/ByteExtensions.cs index 7173d6b..016ffbb 100644 --- a/X10D/src/Math/ByteExtensions.cs +++ b/X10D/src/Math/ByteExtensions.cs @@ -125,6 +125,23 @@ public static class ByteExtensions return ((long)value).IsPrime(); } + /// + /// Calculates the lowest common multiple between the current 8-bit signed integer, and another 8-bit signed integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static byte LowestCommonMultiple(this byte value, byte other) + { + return (byte)((long)value).LowestCommonMultiple(other); + } + /// /// Returns the multiplicative persistence of a specified value. /// diff --git a/X10D/src/Math/Int16Extensions.cs b/X10D/src/Math/Int16Extensions.cs index 49fb97b..3a72b9a 100644 --- a/X10D/src/Math/Int16Extensions.cs +++ b/X10D/src/Math/Int16Extensions.cs @@ -135,6 +135,23 @@ public static class Int16Extensions return ((long)value).IsPrime(); } + /// + /// Calculates the lowest common multiple between the current 16-bit signed integer, and another 16-bit signed integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static short LowestCommonMultiple(this short value, short other) + { + return (short)((long)value).LowestCommonMultiple(other); + } + /// /// Performs a modulo operation which supports a negative dividend. /// diff --git a/X10D/src/Math/Int32Extensions.cs b/X10D/src/Math/Int32Extensions.cs index 17b578d..d654014 100644 --- a/X10D/src/Math/Int32Extensions.cs +++ b/X10D/src/Math/Int32Extensions.cs @@ -135,6 +135,23 @@ public static class Int32Extensions return ((long)value).IsPrime(); } + /// + /// Calculates the lowest common multiple between the current 32-bit signed integer, and another 32-bit signed integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static int LowestCommonMultiple(this int value, int other) + { + return (int)((long)value).LowestCommonMultiple(other); + } + /// /// Performs a modulo operation which supports a negative dividend. /// diff --git a/X10D/src/Math/Int64Extensions.cs b/X10D/src/Math/Int64Extensions.cs index 7dbc890..94fb8d7 100644 --- a/X10D/src/Math/Int64Extensions.cs +++ b/X10D/src/Math/Int64Extensions.cs @@ -164,6 +164,38 @@ public static class Int64Extensions return true; } + /// + /// Calculates the lowest common multiple between the current 64-bit signed integer, and another 64-bit signed integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static long LowestCommonMultiple(this long value, long other) + { + if (value == 0 || other == 0) + { + return 0; + } + + if (value == 1) + { + return other; + } + + if (other == 1) + { + return value; + } + + return value * other / value.GreatestCommonFactor(other); + } + /// /// Performs a modulo operation which supports a negative dividend. /// diff --git a/X10D/src/Math/SByteExtensions.cs b/X10D/src/Math/SByteExtensions.cs index 4e7f564..5bf96c5 100644 --- a/X10D/src/Math/SByteExtensions.cs +++ b/X10D/src/Math/SByteExtensions.cs @@ -136,6 +136,23 @@ public static class SByteExtensions return ((long)value).IsPrime(); } + /// + /// Calculates the lowest common multiple between the current 8-bit signed integer, and another 8-bit signed integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static sbyte LowestCommonMultiple(this sbyte value, sbyte other) + { + return (sbyte)((long)value).LowestCommonMultiple(other); + } + /// /// Performs a modulo operation which supports a negative dividend. /// diff --git a/X10D/src/Math/UInt16Extensions.cs b/X10D/src/Math/UInt16Extensions.cs index 406431f..065afcf 100644 --- a/X10D/src/Math/UInt16Extensions.cs +++ b/X10D/src/Math/UInt16Extensions.cs @@ -131,6 +131,24 @@ public static class UInt16Extensions return !value.IsEven(); } + /// + /// Calculates the lowest common multiple between the current 16-bit unsigned integer, and another 16-bit unsigned + /// integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static ushort LowestCommonMultiple(this ushort value, ushort other) + { + return (ushort)((ulong)value).LowestCommonMultiple(other); + } + /// /// Returns the multiplicative persistence of a specified value. /// diff --git a/X10D/src/Math/UInt32Extensions.cs b/X10D/src/Math/UInt32Extensions.cs index c227484..df2d291 100644 --- a/X10D/src/Math/UInt32Extensions.cs +++ b/X10D/src/Math/UInt32Extensions.cs @@ -131,6 +131,24 @@ public static class UInt32Extensions return !value.IsEven(); } + /// + /// Calculates the lowest common multiple between the current 32-bit unsigned integer, and another 32-bit unsigned + /// integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static uint LowestCommonMultiple(this uint value, uint other) + { + return (uint)((ulong)value).LowestCommonMultiple(other); + } + /// /// Returns the multiplicative persistence of a specified value. /// diff --git a/X10D/src/Math/UInt64Extensions.cs b/X10D/src/Math/UInt64Extensions.cs index 2bdaf1b..ca5c2f5 100644 --- a/X10D/src/Math/UInt64Extensions.cs +++ b/X10D/src/Math/UInt64Extensions.cs @@ -160,6 +160,39 @@ public static class UInt64Extensions return !value.IsEven(); } + /// + /// Calculates the lowest common multiple between the current 64-bit unsigned integer, and another 64-bit unsigned + /// integer. + /// + /// The first value. + /// The second value. + /// The lowest common multiple between and . + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static ulong LowestCommonMultiple(this ulong value, ulong other) + { + if (value == 0 || other == 0) + { + return 0; + } + + if (value == 1) + { + return other; + } + + if (other == 1) + { + return value; + } + + return value * other / value.GreatestCommonFactor(other); + } + /// /// Returns the multiplicative persistence of a specified value. ///