1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:45:42 +00:00

feat: add LowestCommonMultiple for built-in integer types

This was previously incorrectly documented in CHANGELOG.md. The method now exists. Sorry about that
This commit is contained in:
Oliver Booth 2023-03-31 18:07:50 +01:00
parent 9ee99d72d3
commit 275d98fbf8
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
16 changed files with 616 additions and 1 deletions

View File

@ -72,6 +72,56 @@ public class ByteTests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -72,6 +72,68 @@ public class Int16Tests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -72,6 +72,68 @@ public class Int32Tests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -72,6 +72,68 @@ public class Int64Tests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -73,6 +73,68 @@ public class SByteTests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -73,6 +73,56 @@ public class UInt16Tests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -73,6 +73,56 @@ public class UInt32Tests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Math; using X10D.Math;
namespace X10D.Tests.Math; namespace X10D.Tests.Math;
@ -77,6 +77,54 @@ public class UInt64Tests
Assert.IsFalse(two.IsOdd()); 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] [TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders() public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{ {

View File

@ -125,6 +125,23 @@ public static class ByteExtensions
return ((long)value).IsPrime(); return ((long)value).IsPrime();
} }
/// <summary>
/// Calculates the lowest common multiple between the current 8-bit signed integer, and another 8-bit signed integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Returns the multiplicative persistence of a specified value. /// Returns the multiplicative persistence of a specified value.
/// </summary> /// </summary>

View File

@ -135,6 +135,23 @@ public static class Int16Extensions
return ((long)value).IsPrime(); return ((long)value).IsPrime();
} }
/// <summary>
/// Calculates the lowest common multiple between the current 16-bit signed integer, and another 16-bit signed integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Performs a modulo operation which supports a negative dividend. /// Performs a modulo operation which supports a negative dividend.
/// </summary> /// </summary>

View File

@ -135,6 +135,23 @@ public static class Int32Extensions
return ((long)value).IsPrime(); return ((long)value).IsPrime();
} }
/// <summary>
/// Calculates the lowest common multiple between the current 32-bit signed integer, and another 32-bit signed integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Performs a modulo operation which supports a negative dividend. /// Performs a modulo operation which supports a negative dividend.
/// </summary> /// </summary>

View File

@ -164,6 +164,38 @@ public static class Int64Extensions
return true; return true;
} }
/// <summary>
/// Calculates the lowest common multiple between the current 64-bit signed integer, and another 64-bit signed integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Performs a modulo operation which supports a negative dividend. /// Performs a modulo operation which supports a negative dividend.
/// </summary> /// </summary>

View File

@ -136,6 +136,23 @@ public static class SByteExtensions
return ((long)value).IsPrime(); return ((long)value).IsPrime();
} }
/// <summary>
/// Calculates the lowest common multiple between the current 8-bit signed integer, and another 8-bit signed integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Performs a modulo operation which supports a negative dividend. /// Performs a modulo operation which supports a negative dividend.
/// </summary> /// </summary>

View File

@ -131,6 +131,24 @@ public static class UInt16Extensions
return !value.IsEven(); return !value.IsEven();
} }
/// <summary>
/// Calculates the lowest common multiple between the current 16-bit unsigned integer, and another 16-bit unsigned
/// integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Returns the multiplicative persistence of a specified value. /// Returns the multiplicative persistence of a specified value.
/// </summary> /// </summary>

View File

@ -131,6 +131,24 @@ public static class UInt32Extensions
return !value.IsEven(); return !value.IsEven();
} }
/// <summary>
/// Calculates the lowest common multiple between the current 32-bit unsigned integer, and another 32-bit unsigned
/// integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Returns the multiplicative persistence of a specified value. /// Returns the multiplicative persistence of a specified value.
/// </summary> /// </summary>

View File

@ -160,6 +160,39 @@ public static class UInt64Extensions
return !value.IsEven(); return !value.IsEven();
} }
/// <summary>
/// Calculates the lowest common multiple between the current 64-bit unsigned integer, and another 64-bit unsigned
/// integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[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);
}
/// <summary> /// <summary>
/// Returns the multiplicative persistence of a specified value. /// Returns the multiplicative persistence of a specified value.
/// </summary> /// </summary>