From 275d98fbf82b06aa0e9b5e6a728d9297572c894a Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 31 Mar 2023 18:07:50 +0100 Subject: [PATCH] feat: add LowestCommonMultiple for built-in integer types This was previously incorrectly documented in CHANGELOG.md. The method now exists. Sorry about that --- X10D.Tests/src/Math/ByteTests.cs | 50 ++++++++++++++++++++++++ X10D.Tests/src/Math/Int16Tests.cs | 62 ++++++++++++++++++++++++++++++ X10D.Tests/src/Math/Int32Tests.cs | 62 ++++++++++++++++++++++++++++++ X10D.Tests/src/Math/Int64Tests.cs | 62 ++++++++++++++++++++++++++++++ X10D.Tests/src/Math/SByteTests.cs | 62 ++++++++++++++++++++++++++++++ X10D.Tests/src/Math/UInt16Tests.cs | 50 ++++++++++++++++++++++++ X10D.Tests/src/Math/UInt32Tests.cs | 50 ++++++++++++++++++++++++ X10D.Tests/src/Math/UInt64Tests.cs | 50 +++++++++++++++++++++++- X10D/src/Math/ByteExtensions.cs | 17 ++++++++ X10D/src/Math/Int16Extensions.cs | 17 ++++++++ X10D/src/Math/Int32Extensions.cs | 17 ++++++++ X10D/src/Math/Int64Extensions.cs | 32 +++++++++++++++ X10D/src/Math/SByteExtensions.cs | 17 ++++++++ X10D/src/Math/UInt16Extensions.cs | 18 +++++++++ X10D/src/Math/UInt32Extensions.cs | 18 +++++++++ X10D/src/Math/UInt64Extensions.cs | 33 ++++++++++++++++ 16 files changed, 616 insertions(+), 1 deletion(-) diff --git a/X10D.Tests/src/Math/ByteTests.cs b/X10D.Tests/src/Math/ByteTests.cs index f95568261..854d4b9ce 100644 --- a/X10D.Tests/src/Math/ByteTests.cs +++ b/X10D.Tests/src/Math/ByteTests.cs @@ -72,6 +72,56 @@ public void IsOddShouldBeCorrect() 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 3c7818379..f2f8a3126 100644 --- a/X10D.Tests/src/Math/Int16Tests.cs +++ b/X10D.Tests/src/Math/Int16Tests.cs @@ -72,6 +72,68 @@ public void IsOddShouldBeCorrect() 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 c58f71a54..ef8e45bdc 100644 --- a/X10D.Tests/src/Math/Int32Tests.cs +++ b/X10D.Tests/src/Math/Int32Tests.cs @@ -72,6 +72,68 @@ public void IsOddShouldBeCorrect() 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 ee19d8fd1..752d9d633 100644 --- a/X10D.Tests/src/Math/Int64Tests.cs +++ b/X10D.Tests/src/Math/Int64Tests.cs @@ -72,6 +72,68 @@ public void IsOddShouldBeCorrect() 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 116c1d8f9..2664a6313 100644 --- a/X10D.Tests/src/Math/SByteTests.cs +++ b/X10D.Tests/src/Math/SByteTests.cs @@ -73,6 +73,68 @@ public void IsOddShouldBeCorrect() 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 dd98c88e9..4d74154d3 100644 --- a/X10D.Tests/src/Math/UInt16Tests.cs +++ b/X10D.Tests/src/Math/UInt16Tests.cs @@ -73,6 +73,56 @@ public void IsOddShouldBeCorrect() 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 2a7cc548f..1573b9b36 100644 --- a/X10D.Tests/src/Math/UInt32Tests.cs +++ b/X10D.Tests/src/Math/UInt32Tests.cs @@ -73,6 +73,56 @@ public void IsOddShouldBeCorrect() 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 b0e5e545a..35e8d4ca4 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 void IsOddShouldBeCorrect() 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 7173d6bee..016ffbbfe 100644 --- a/X10D/src/Math/ByteExtensions.cs +++ b/X10D/src/Math/ByteExtensions.cs @@ -125,6 +125,23 @@ public static bool IsPrime(this byte value) 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 49fb97bc0..3a72b9af8 100644 --- a/X10D/src/Math/Int16Extensions.cs +++ b/X10D/src/Math/Int16Extensions.cs @@ -135,6 +135,23 @@ public static bool IsPrime(this short value) 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 17b578da4..d654014a9 100644 --- a/X10D/src/Math/Int32Extensions.cs +++ b/X10D/src/Math/Int32Extensions.cs @@ -135,6 +135,23 @@ public static bool IsPrime(this int value) 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 7dbc890d1..94fb8d7cd 100644 --- a/X10D/src/Math/Int64Extensions.cs +++ b/X10D/src/Math/Int64Extensions.cs @@ -164,6 +164,38 @@ public static bool IsPrime(this long value) 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 4e7f56499..5bf96c53f 100644 --- a/X10D/src/Math/SByteExtensions.cs +++ b/X10D/src/Math/SByteExtensions.cs @@ -136,6 +136,23 @@ public static bool IsPrime(this sbyte value) 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 406431f3c..065afcfac 100644 --- a/X10D/src/Math/UInt16Extensions.cs +++ b/X10D/src/Math/UInt16Extensions.cs @@ -131,6 +131,24 @@ public static bool IsOdd(this ushort value) 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 c227484be..df2d29179 100644 --- a/X10D/src/Math/UInt32Extensions.cs +++ b/X10D/src/Math/UInt32Extensions.cs @@ -131,6 +131,24 @@ public static bool IsOdd(this uint value) 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 2bdaf1b49..ca5c2f518 100644 --- a/X10D/src/Math/UInt64Extensions.cs +++ b/X10D/src/Math/UInt64Extensions.cs @@ -160,6 +160,39 @@ public static bool IsOdd(this ulong value) 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. ///