1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:18:47 +00:00

Add GreatestCommonFactor for built-in integer types

This commit is contained in:
Oliver Booth 2023-02-26 12:24:51 +00:00
parent 2aab9e8d6a
commit ca1b1ccbf2
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
17 changed files with 329 additions and 3 deletions

View File

@ -13,6 +13,7 @@
- X10D: Added `Color.GetClosestConsoleColor()` - X10D: Added `Color.GetClosestConsoleColor()`
- X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()` - X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()`
- X10D: Added `DirectoryInfo.Clear()` - X10D: Added `DirectoryInfo.Clear()`
- X10D: Added `GreatestCommonFactor` for built-in integer types
- X10D: Added `IEnumerable<T>.CountWhereNot(Func<T, bool>)` - X10D: Added `IEnumerable<T>.CountWhereNot(Func<T, bool>)`
- X10D: Added `IEnumerable<T>.FirstWhereNot(Func<T, bool>)` - X10D: Added `IEnumerable<T>.FirstWhereNot(Func<T, bool>)`
- X10D: Added `IEnumerable<T>.FirstWhereNotOrDefault(Func<T, bool>)` - X10D: Added `IEnumerable<T>.FirstWhereNotOrDefault(Func<T, bool>)`

View File

@ -30,6 +30,28 @@ public class ByteTests
Assert.AreEqual(3628800L, ((byte)10).Factorial()); Assert.AreEqual(3628800L, ((byte)10).Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const byte first = 5;
const byte second = 7;
byte multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const byte first = 12;
const byte second = 18;
byte multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -30,6 +30,28 @@ public class Int16Tests
Assert.AreEqual(3628800L, ((short)10).Factorial()); Assert.AreEqual(3628800L, ((short)10).Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const short first = 5;
const short second = 7;
short multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const short first = 12;
const short second = 18;
short multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -30,6 +30,28 @@ public class Int32Tests
Assert.AreEqual(3628800L, 10.Factorial()); Assert.AreEqual(3628800L, 10.Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const int first = 5;
const int second = 7;
int multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const int first = 12;
const int second = 18;
int multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -30,6 +30,28 @@ public class Int64Tests
Assert.AreEqual(3628800L, 10L.Factorial()); Assert.AreEqual(3628800L, 10L.Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const long first = 5L;
const long second = 7L;
long multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1L, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const long first = 12L;
const long second = 18L;
long multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6L, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -31,6 +31,28 @@ public class SByteTests
Assert.AreEqual(3628800L, ((sbyte)10).Factorial()); Assert.AreEqual(3628800L, ((sbyte)10).Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const sbyte first = 5;
const sbyte second = 7;
sbyte multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const sbyte first = 12;
const sbyte second = 18;
sbyte multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -31,6 +31,28 @@ public class UInt16Tests
Assert.AreEqual(3628800UL, ((ushort)10).Factorial()); Assert.AreEqual(3628800UL, ((ushort)10).Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const ushort first = 5;
const ushort second = 7;
ushort multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const ushort first = 12;
const ushort second = 18;
ushort multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -31,6 +31,28 @@ public class UInt32Tests
Assert.AreEqual(3628800UL, 10U.Factorial()); Assert.AreEqual(3628800UL, 10U.Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const uint first = 5U;
const uint second = 7U;
uint multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1U, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const uint first = 12U;
const uint second = 18U;
uint multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6U, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -35,6 +35,28 @@ public class UInt64Tests
Assert.AreEqual(3628800UL, 10UL.Factorial()); Assert.AreEqual(3628800UL, 10UL.Factorial());
} }
[TestMethod]
public void GreatestCommonFactor_ShouldBe1_ForPrimeNumbers()
{
const ulong first = 5UL;
const ulong second = 7UL;
ulong multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(1UL, multiple);
}
[TestMethod]
public void GreatestCommonFactor_ShouldBe6_Given12And18()
{
const ulong first = 12UL;
const ulong second = 18UL;
ulong multiple = first.GreatestCommonFactor(second);
Assert.AreEqual(6UL, multiple);
}
[TestMethod] [TestMethod]
public void IsEvenShouldBeCorrect() public void IsEvenShouldBeCorrect()
{ {

View File

@ -1,4 +1,4 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace X10D.Math; namespace X10D.Math;
@ -57,6 +57,23 @@ public static class ByteExtensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor between the current 8-bit unsigned integer, and another 8-bit unsigned integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The greatest common factor 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 GreatestCommonFactor(this byte value, byte other)
{
return (byte)((long)value).GreatestCommonFactor(other);
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>

View File

@ -1,4 +1,4 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace X10D.Math; namespace X10D.Math;
@ -62,6 +62,23 @@ public static class Int16Extensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor 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 greatest common factor 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 GreatestCommonFactor(this short value, short other)
{
return (short)((long)value).GreatestCommonFactor(other);
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>

View File

@ -1,4 +1,4 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace X10D.Math; namespace X10D.Math;
@ -62,6 +62,23 @@ public static class Int32Extensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor 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 greatest common factor 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 GreatestCommonFactor(this int value, int other)
{
return (int)((long)value).GreatestCommonFactor(other);
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>

View File

@ -62,6 +62,28 @@ public static class Int64Extensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor between the current 64-bit signed integer, and another 64-bit unsigned integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The greatest common factor 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 GreatestCommonFactor(this long value, long other)
{
while (other != 0)
{
(value, other) = (other, value % other);
}
return value;
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>

View File

@ -63,6 +63,23 @@ public static class SByteExtensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor 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 greatest common factor 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 GreatestCommonFactor(this sbyte value, sbyte other)
{
return (sbyte)((long)value).GreatestCommonFactor(other);
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>

View File

@ -57,6 +57,24 @@ public static class UInt16Extensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor 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 greatest common factor 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 GreatestCommonFactor(this ushort value, ushort other)
{
return (ushort)((long)value).GreatestCommonFactor(other);
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>

View File

@ -57,6 +57,24 @@ public static class UInt32Extensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor 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 greatest common factor 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 GreatestCommonFactor(this uint value, uint other)
{
return (uint)((long)value).GreatestCommonFactor(other);
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>

View File

@ -57,6 +57,29 @@ public static class UInt64Extensions
return result; return result;
} }
/// <summary>
/// Calculates the greatest common factor 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 greatest common factor 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 GreatestCommonFactor(this ulong value, ulong other)
{
while (other != 0)
{
(value, other) = (other, value % other);
}
return value;
}
/// <summary> /// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2. /// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary> /// </summary>