Skip to content

Commit

Permalink
refactor!: move Factorial to BinaryIntegerExtensions for .net>=7
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Feb 17, 2024
1 parent eb46257 commit 30f158e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 8 deletions.
30 changes: 30 additions & 0 deletions X10D/src/Math/BinaryIntegerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,35 @@ public static int DigitalRoot<TInteger>(this TInteger value)
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
2 changes: 1 addition & 1 deletion X10D/src/Math/ByteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static byte DigitalRoot(this byte value)
int root = value % 9;
return (byte)(root == 0 ? 9 : root);
}
#endif

/// <summary>
/// Returns the factorial of the current 8-bit unsigned integer.
Expand All @@ -66,6 +65,7 @@ public static long Factorial(this byte value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 8-bit unsigned integer, and another 8-bit unsigned integer.
Expand Down
2 changes: 1 addition & 1 deletion X10D/src/Math/Int16Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>
/// Returns the factorial of the current 16-bit signed integer.
Expand Down Expand Up @@ -71,6 +70,7 @@ public static long Factorial(this short value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 16-bit signed integer, and another 16-bit signed integer.
Expand Down
2 changes: 1 addition & 1 deletion X10D/src/Math/Int32Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>
/// Returns the factorial of the current 32-bit signed integer.
Expand Down Expand Up @@ -71,6 +70,7 @@ public static long Factorial(this int value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 32-bit signed integer, and another 32-bit signed integer.
Expand Down
2 changes: 1 addition & 1 deletion X10D/src/Math/Int64Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>
/// Returns the factorial of the current 64-bit signed integer.
Expand Down Expand Up @@ -71,6 +70,7 @@ public static long Factorial(this long value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 64-bit signed integer, and another 64-bit unsigned integer.
Expand Down
2 changes: 1 addition & 1 deletion X10D/src/Math/SByteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>
/// Returns the factorial of the current 8-bit signed integer.
Expand Down Expand Up @@ -72,6 +71,7 @@ public static long Factorial(this sbyte value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 8-bit signed integer, and another 8-bit signed integer.
Expand Down
2 changes: 1 addition & 1 deletion X10D/src/Math/UInt16Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static ushort DigitalRoot(this ushort value)
var root = (ushort)(value % 9);
return (ushort)(root == 0 ? 9 : root);
}
#endif

/// <summary>
/// Returns the factorial of the current 16-bit unsigned integer.
Expand All @@ -66,6 +65,7 @@ public static ulong Factorial(this ushort value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 16-bit unsigned integer, and another 16-bit unsigned
Expand Down
2 changes: 1 addition & 1 deletion X10D/src/Math/UInt32Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static uint DigitalRoot(this uint value)
uint root = value % 9;
return root == 0 ? 9 : root;
}
#endif

/// <summary>
/// Returns the factorial of the current 32-bit unsigned integer.
Expand All @@ -66,6 +65,7 @@ public static ulong Factorial(this uint value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 32-bit unsigned integer, and another 32-bit unsigned
Expand Down
2 changes: 1 addition & 1 deletion X10D/src/Math/UInt64Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static ulong DigitalRoot(this ulong value)
ulong root = value % 9;
return root == 0 ? 9 : root;
}
#endif

/// <summary>
/// Returns the factorial of the current 64-bit unsigned integer.
Expand All @@ -66,6 +65,7 @@ public static ulong Factorial(this ulong value)

return result;
}
#endif

/// <summary>
/// Calculates the greatest common factor between the current 64-bit unsigned integer, and another 64-bit unsigned
Expand Down

0 comments on commit 30f158e

Please sign in to comment.