refactor!: move Factorial to BinaryIntegerExtensions for .net>=7

This commit is contained in:
Oliver Booth 2024-02-17 18:13:45 +00:00
parent eb46257b75
commit 30f158e861
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
9 changed files with 38 additions and 8 deletions

View File

@ -48,5 +48,35 @@ public static class BinaryIntegerExtensions
TInteger root = TInteger.Abs(value).Mod(nine);
return int.CreateChecked(root == TInteger.Zero ? nine : root);
}
/// <summary>
/// Returns the factorial of the current binary integer.
/// </summary>
/// <param name="value">The value whose factorial to compute.</param>
/// <returns>The factorial of <paramref name="value" />.</returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is less than 0.</exception>
[Pure]
[MethodImpl(CompilerResources.MaxOptimization)]
public static long Factorial<TInteger>(this TInteger value)
where TInteger : IBinaryInteger<TInteger>
{
if (value < TInteger.Zero)
{
throw new ArithmeticException(nameof(value));
}
if (value == TInteger.Zero)
{
return 1;
}
var result = 1L;
for (TInteger i = TInteger.One; i <= value; i++)
{
result *= long.CreateChecked(i);
}
return result;
}
}
#endif

View File

@ -42,7 +42,6 @@ public static class ByteExtensions
int root = value % 9;
return (byte)(root == 0 ? 9 : root);
}
#endif
/// <summary>
/// Returns the factorial of the current 8-bit unsigned integer.
@ -66,6 +65,7 @@ public static class ByteExtensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 8-bit unsigned integer, and another 8-bit unsigned integer.

View File

@ -41,7 +41,6 @@ public static class Int16Extensions
short root = System.Math.Abs(value).Mod(9);
return root < 1 ? (short)(9 - root) : root;
}
#endif
/// <summary>
/// Returns the factorial of the current 16-bit signed integer.
@ -71,6 +70,7 @@ public static class Int16Extensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 16-bit signed integer, and another 16-bit signed integer.

View File

@ -41,7 +41,6 @@ public static class Int32Extensions
int root = System.Math.Abs(value).Mod(9);
return root < 1 ? 9 - root : root;
}
#endif
/// <summary>
/// Returns the factorial of the current 32-bit signed integer.
@ -71,6 +70,7 @@ public static class Int32Extensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 32-bit signed integer, and another 32-bit signed integer.

View File

@ -41,7 +41,6 @@ public static class Int64Extensions
long root = System.Math.Abs(value).Mod(9L);
return root < 1L ? 9L - root : root;
}
#endif
/// <summary>
/// Returns the factorial of the current 64-bit signed integer.
@ -71,6 +70,7 @@ public static class Int64Extensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 64-bit signed integer, and another 64-bit unsigned integer.

View File

@ -42,7 +42,6 @@ public static class SByteExtensions
int root = System.Math.Abs(value).Mod(9);
return (sbyte)(root < 1 ? 9 - root : root);
}
#endif
/// <summary>
/// Returns the factorial of the current 8-bit signed integer.
@ -72,6 +71,7 @@ public static class SByteExtensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 8-bit signed integer, and another 8-bit signed integer.

View File

@ -42,7 +42,6 @@ public static class UInt16Extensions
var root = (ushort)(value % 9);
return (ushort)(root == 0 ? 9 : root);
}
#endif
/// <summary>
/// Returns the factorial of the current 16-bit unsigned integer.
@ -66,6 +65,7 @@ public static class UInt16Extensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 16-bit unsigned integer, and another 16-bit unsigned

View File

@ -42,7 +42,6 @@ public static class UInt32Extensions
uint root = value % 9;
return root == 0 ? 9 : root;
}
#endif
/// <summary>
/// Returns the factorial of the current 32-bit unsigned integer.
@ -66,6 +65,7 @@ public static class UInt32Extensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 32-bit unsigned integer, and another 32-bit unsigned

View File

@ -42,7 +42,6 @@ public static class UInt64Extensions
ulong root = value % 9;
return root == 0 ? 9 : root;
}
#endif
/// <summary>
/// Returns the factorial of the current 64-bit unsigned integer.
@ -66,6 +65,7 @@ public static class UInt64Extensions
return result;
}
#endif
/// <summary>
/// Calculates the greatest common factor between the current 64-bit unsigned integer, and another 64-bit unsigned