@@ -7,31 +7,31 @@ namespace SharpCoding.SharpHelpers
77 public static class NumericHelper
88 {
99 /// <summary>
10- /// check if the int number that invokes the method is odd
10+ /// Checks if the integer number is odd.
1111 /// </summary>
12- /// <param name="numberToCheck"></param>
13- /// <returns></returns>
12+ /// <param name="numberToCheck">The integer number to check. </param>
13+ /// <returns>True if the number is odd; otherwise, false. </returns>
1414 public static bool IsOdd ( this int numberToCheck )
1515 {
1616 var restOfDivision = ( numberToCheck % 2 ) ;
1717 return ( restOfDivision == 1 ) ;
1818 }
1919
2020 /// <summary>
21- /// check if the int number that invokes the method is even
21+ /// Checks if the integer number is even.
2222 /// </summary>
23- /// <param name="numberToCheck"></param>
24- /// <returns></returns>
23+ /// <param name="numberToCheck">The integer number to check. </param>
24+ /// <returns>True if the number is even; otherwise, false. </returns>
2525 public static bool IsEven ( this int numberToCheck )
2626 {
2727 return ! numberToCheck . IsOdd ( ) ;
2828 }
2929
3030 /// <summary>
31- /// check if the int number that invokes the method is prime
31+ /// Checks if the integer number is prime.
3232 /// </summary>
33- /// <param name="numberToCheck"></param>
34- /// <returns></returns>
33+ /// <param name="numberToCheck">The integer number to check. </param>
34+ /// <returns>True if the number is prime; otherwise, false. </returns>
3535 public static bool IsPrime ( this int numberToCheck )
3636 {
3737 var limit = Math . Ceiling ( Math . Sqrt ( numberToCheck ) ) ;
@@ -44,5 +44,89 @@ public static bool IsPrime(this int numberToCheck)
4444 }
4545 return true ;
4646 }
47+
48+ /// <summary>
49+ /// Calculates the factorial of the integer number.
50+ /// </summary>
51+ /// <param name="number">The integer number to calculate the factorial for.</param>
52+ /// <returns>The factorial of the number.</returns>
53+ /// <exception cref="ArgumentException">Thrown when the number is negative.</exception>
54+ public static long Factorial ( this int number )
55+ {
56+ if ( number < 0 ) throw new ArgumentException ( "Number must be non-negative." ) ;
57+ return number <= 1 ? 1 : number * Factorial ( number - 1 ) ;
58+ }
59+
60+ /// <summary>
61+ /// Computes the Greatest Common Divisor (GCD) of two integers.
62+ /// </summary>
63+ /// <param name="a">The first integer.</param>
64+ /// <param name="b">The second integer.</param>
65+ /// <returns>The greatest common divisor of the two numbers.</returns>
66+ public static int GCD ( this int a , int b )
67+ {
68+ while ( b != 0 )
69+ {
70+ int temp = b ;
71+ b = a % b ;
72+ a = temp ;
73+ }
74+ return a ;
75+ }
76+
77+ /// <summary>
78+ /// Computes the Least Common Multiple (LCM) of two integers.
79+ /// </summary>
80+ /// <param name="a">The first integer.</param>
81+ /// <param name="b">The second integer.</param>
82+ /// <returns>The least common multiple of the two numbers.</returns>
83+ public static int LCM ( this int a , int b )
84+ {
85+ return Math . Abs ( a * b ) / a . GCD ( b ) ;
86+ }
87+
88+ /// <summary>
89+ /// Checks if the integer number is negative.
90+ /// </summary>
91+ /// <param name="number">The integer number to check.</param>
92+ /// <returns>True if the number is negative; otherwise, false.</returns>
93+ public static bool IsNegative ( this int number )
94+ {
95+ return number < 0 ;
96+ }
97+
98+ /// <summary>
99+ /// Checks if the integer number is positive.
100+ /// </summary>
101+ /// <param name="number">The integer number to check.</param>
102+ /// <returns>True if the number is positive; otherwise, false.</returns>
103+ public static bool IsPositive ( this int number )
104+ {
105+ return number > 0 ;
106+ }
107+
108+ /// <summary>
109+ /// Clamps the integer number within the specified range.
110+ /// </summary>
111+ /// <param name="value">The integer number to clamp.</param>
112+ /// <param name="min">The minimum value.</param>
113+ /// <param name="max">The maximum value.</param>
114+ /// <returns>The clamped value within the specified range.</returns>
115+ public static int Clamp ( this int value , int min , int max )
116+ {
117+ if ( value < min ) return min ;
118+ if ( value > max ) return max ;
119+ return value ;
120+ }
121+
122+ /// <summary>
123+ /// Returns the absolute value of the integer number.
124+ /// </summary>
125+ /// <param name="number">The integer number to get the absolute value for.</param>
126+ /// <returns>The absolute value of the number.</returns>
127+ public static int Abs ( this int number )
128+ {
129+ return Math . Abs ( number ) ;
130+ }
47131 }
48132}
0 commit comments