diff --git a/X10D/src/Math/BinaryIntegerExtensions.cs b/X10D/src/Math/BinaryIntegerExtensions.cs index 5b460c9fe..786c52ae3 100644 --- a/X10D/src/Math/BinaryIntegerExtensions.cs +++ b/X10D/src/Math/BinaryIntegerExtensions.cs @@ -48,5 +48,35 @@ public static int DigitalRoot(this TInteger value) TInteger root = TInteger.Abs(value).Mod(nine); return int.CreateChecked(root == TInteger.Zero ? nine : root); } + + /// + /// Returns the factorial of the current binary integer. + /// + /// The value whose factorial to compute. + /// The factorial of . + /// is less than 0. + [Pure] + [MethodImpl(CompilerResources.MaxOptimization)] + public static long Factorial(this TInteger value) + where TInteger : IBinaryInteger + { + 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 diff --git a/X10D/src/Math/ByteExtensions.cs b/X10D/src/Math/ByteExtensions.cs index 500988d59..157994efb 100644 --- a/X10D/src/Math/ByteExtensions.cs +++ b/X10D/src/Math/ByteExtensions.cs @@ -42,7 +42,6 @@ public static byte DigitalRoot(this byte value) int root = value % 9; return (byte)(root == 0 ? 9 : root); } -#endif /// /// Returns the factorial of the current 8-bit unsigned integer. @@ -66,6 +65,7 @@ public static long Factorial(this byte value) return result; } +#endif /// /// Calculates the greatest common factor between the current 8-bit unsigned integer, and another 8-bit unsigned integer. diff --git a/X10D/src/Math/Int16Extensions.cs b/X10D/src/Math/Int16Extensions.cs index dc3cf96de..40d633fab 100644 --- a/X10D/src/Math/Int16Extensions.cs +++ b/X10D/src/Math/Int16Extensions.cs @@ -41,7 +41,6 @@ public static short DigitalRoot(this short value) short root = System.Math.Abs(value).Mod(9); return root < 1 ? (short)(9 - root) : root; } -#endif /// /// Returns the factorial of the current 16-bit signed integer. @@ -71,6 +70,7 @@ public static long Factorial(this short value) return result; } +#endif /// /// Calculates the greatest common factor between the current 16-bit signed integer, and another 16-bit signed integer. diff --git a/X10D/src/Math/Int32Extensions.cs b/X10D/src/Math/Int32Extensions.cs index 7ed65350d..3d2c8587a 100644 --- a/X10D/src/Math/Int32Extensions.cs +++ b/X10D/src/Math/Int32Extensions.cs @@ -41,7 +41,6 @@ public static int DigitalRoot(this int value) int root = System.Math.Abs(value).Mod(9); return root < 1 ? 9 - root : root; } -#endif /// /// Returns the factorial of the current 32-bit signed integer. @@ -71,6 +70,7 @@ public static long Factorial(this int value) return result; } +#endif /// /// Calculates the greatest common factor between the current 32-bit signed integer, and another 32-bit signed integer. diff --git a/X10D/src/Math/Int64Extensions.cs b/X10D/src/Math/Int64Extensions.cs index 64b94e0cc..0dcc43fd5 100644 --- a/X10D/src/Math/Int64Extensions.cs +++ b/X10D/src/Math/Int64Extensions.cs @@ -41,7 +41,6 @@ public static long DigitalRoot(this long value) long root = System.Math.Abs(value).Mod(9L); return root < 1L ? 9L - root : root; } -#endif /// /// Returns the factorial of the current 64-bit signed integer. @@ -71,6 +70,7 @@ public static long Factorial(this long value) return result; } +#endif /// /// Calculates the greatest common factor between the current 64-bit signed integer, and another 64-bit unsigned integer. diff --git a/X10D/src/Math/SByteExtensions.cs b/X10D/src/Math/SByteExtensions.cs index 00cfa04ba..17e536b24 100644 --- a/X10D/src/Math/SByteExtensions.cs +++ b/X10D/src/Math/SByteExtensions.cs @@ -42,7 +42,6 @@ public static sbyte DigitalRoot(this sbyte value) int root = System.Math.Abs(value).Mod(9); return (sbyte)(root < 1 ? 9 - root : root); } -#endif /// /// Returns the factorial of the current 8-bit signed integer. @@ -72,6 +71,7 @@ public static long Factorial(this sbyte value) return result; } +#endif /// /// Calculates the greatest common factor between the current 8-bit signed integer, and another 8-bit signed integer. diff --git a/X10D/src/Math/UInt16Extensions.cs b/X10D/src/Math/UInt16Extensions.cs index 8479cb203..090808a81 100644 --- a/X10D/src/Math/UInt16Extensions.cs +++ b/X10D/src/Math/UInt16Extensions.cs @@ -42,7 +42,6 @@ public static ushort DigitalRoot(this ushort value) var root = (ushort)(value % 9); return (ushort)(root == 0 ? 9 : root); } -#endif /// /// Returns the factorial of the current 16-bit unsigned integer. @@ -66,6 +65,7 @@ public static ulong Factorial(this ushort value) return result; } +#endif /// /// Calculates the greatest common factor between the current 16-bit unsigned integer, and another 16-bit unsigned diff --git a/X10D/src/Math/UInt32Extensions.cs b/X10D/src/Math/UInt32Extensions.cs index 6684e4fa7..700e32ba8 100644 --- a/X10D/src/Math/UInt32Extensions.cs +++ b/X10D/src/Math/UInt32Extensions.cs @@ -42,7 +42,6 @@ public static uint DigitalRoot(this uint value) uint root = value % 9; return root == 0 ? 9 : root; } -#endif /// /// Returns the factorial of the current 32-bit unsigned integer. @@ -66,6 +65,7 @@ public static ulong Factorial(this uint value) return result; } +#endif /// /// Calculates the greatest common factor between the current 32-bit unsigned integer, and another 32-bit unsigned diff --git a/X10D/src/Math/UInt64Extensions.cs b/X10D/src/Math/UInt64Extensions.cs index dc1e6f160..3353ab9cd 100644 --- a/X10D/src/Math/UInt64Extensions.cs +++ b/X10D/src/Math/UInt64Extensions.cs @@ -42,7 +42,6 @@ public static ulong DigitalRoot(this ulong value) ulong root = value % 9; return root == 0 ? 9 : root; } -#endif /// /// Returns the factorial of the current 64-bit unsigned integer. @@ -66,6 +65,7 @@ public static ulong Factorial(this ulong value) return result; } +#endif /// /// Calculates the greatest common factor between the current 64-bit unsigned integer, and another 64-bit unsigned