Skip to content

Commit

Permalink
Add some numeric methods
Browse files Browse the repository at this point in the history
  • Loading branch information
engineering87 committed Oct 6, 2024
1 parent 20408bf commit a65bd5a
Showing 1 changed file with 93 additions and 9 deletions.
102 changes: 93 additions & 9 deletions SharpHelpers/SharpHelpers/NumericHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ namespace SharpCoding.SharpHelpers
public static class NumericHelper
{
/// <summary>
/// check if the int number that invokes the method is odd
/// Checks if the integer number is odd.
/// </summary>
/// <param name="numberToCheck"></param>
/// <returns></returns>
/// <param name="numberToCheck">The integer number to check.</param>
/// <returns>True if the number is odd; otherwise, false.</returns>
public static bool IsOdd(this int numberToCheck)
{
var restOfDivision = (numberToCheck % 2);
return (restOfDivision == 1);
}

/// <summary>
/// check if the int number that invokes the method is even
/// Checks if the integer number is even.
/// </summary>
/// <param name="numberToCheck"></param>
/// <returns></returns>
/// <param name="numberToCheck">The integer number to check.</param>
/// <returns>True if the number is even; otherwise, false.</returns>
public static bool IsEven(this int numberToCheck)
{
return !numberToCheck.IsOdd();
}

/// <summary>
/// check if the int number that invokes the method is prime
/// Checks if the integer number is prime.
/// </summary>
/// <param name="numberToCheck"></param>
/// <returns></returns>
/// <param name="numberToCheck">The integer number to check.</param>
/// <returns>True if the number is prime; otherwise, false.</returns>
public static bool IsPrime(this int numberToCheck)
{
var limit = Math.Ceiling(Math.Sqrt(numberToCheck));
Expand All @@ -44,5 +44,89 @@ public static bool IsPrime(this int numberToCheck)
}
return true;
}

/// <summary>
/// Calculates the factorial of the integer number.
/// </summary>
/// <param name="number">The integer number to calculate the factorial for.</param>
/// <returns>The factorial of the number.</returns>
/// <exception cref="ArgumentException">Thrown when the number is negative.</exception>
public static long Factorial(this int number)
{
if (number < 0) throw new ArgumentException("Number must be non-negative.");
return number <= 1 ? 1 : number * Factorial(number - 1);
}

/// <summary>
/// Computes the Greatest Common Divisor (GCD) of two integers.
/// </summary>
/// <param name="a">The first integer.</param>
/// <param name="b">The second integer.</param>
/// <returns>The greatest common divisor of the two numbers.</returns>
public static int GCD(this int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}

/// <summary>
/// Computes the Least Common Multiple (LCM) of two integers.
/// </summary>
/// <param name="a">The first integer.</param>
/// <param name="b">The second integer.</param>
/// <returns>The least common multiple of the two numbers.</returns>
public static int LCM(this int a, int b)
{
return Math.Abs(a * b) / a.GCD(b);
}

/// <summary>
/// Checks if the integer number is negative.
/// </summary>
/// <param name="number">The integer number to check.</param>
/// <returns>True if the number is negative; otherwise, false.</returns>
public static bool IsNegative(this int number)
{
return number < 0;
}

/// <summary>
/// Checks if the integer number is positive.
/// </summary>
/// <param name="number">The integer number to check.</param>
/// <returns>True if the number is positive; otherwise, false.</returns>
public static bool IsPositive(this int number)
{
return number > 0;
}

/// <summary>
/// Clamps the integer number within the specified range.
/// </summary>
/// <param name="value">The integer number to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The clamped value within the specified range.</returns>
public static int Clamp(this int value, int min, int max)
{
if (value < min) return min;
if (value > max) return max;
return value;
}

/// <summary>
/// Returns the absolute value of the integer number.
/// </summary>
/// <param name="number">The integer number to get the absolute value for.</param>
/// <returns>The absolute value of the number.</returns>
public static int Abs(this int number)
{
return Math.Abs(number);
}
}
}

0 comments on commit a65bd5a

Please sign in to comment.