Class BigIntegerExtensions
Provides a set of extension methods for the BigInteger struct, enabling additional functionality such as determining whether a number is odd, computing its absolute value, and calculating its bit length. It also provides the Extended Euclidean algorithm for computing GCD and Bézout coefficients.
Inherited Members
Namespace: MathLib
Assembly: MathLib.dll
Syntax
public static class BigIntegerExtensions
Methods
| Edit this page View SourceAbs(BigInteger)
Absolute value of the specified integer
value.
Declaration
public static BigInteger Abs(this BigInteger integer)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | The BigInteger value whose absolute value is to be computed. |
Returns
Type | Description |
---|---|
BigInteger | A BigInteger representing the absolute value of the input. |
Coprime(BigInteger, BigInteger)
Indicates whether two specified BigInteger values are coprime.
Declaration
public static bool Coprime(this BigInteger a, BigInteger b)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | a | The first BigInteger value. |
BigInteger | b | The second BigInteger value. |
Returns
Type | Description |
---|---|
bool | true
|
ExtendedEuclideanAlgorithm(BigInteger, BigInteger)
Implements the extended Euclidean algorithm to compute the greatest common divisor (GCD) of two BigInteger values and the corresponding Bézout coefficients.
Declaration
public static (BigInteger gcd, BigInteger x, BigInteger y) ExtendedEuclideanAlgorithm(BigInteger a, BigInteger b)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | a | The first BigInteger. |
BigInteger | b | The second BigInteger. |
Returns
Type | Description |
---|---|
(BigInteger gcd, BigInteger x, BigInteger y) | A tuple containing three BigInteger values:
|
Remarks
For two integers a
and b
, this method computes the GCD, denoted as gcd(a, b),
and finds integers 'x' and 'y' such that Bézout's identity is satisfied:
a·x + b· y = gcd(a, b)
IsDivisibleBy(BigInteger, BigInteger)
Indicates whether the specified BigInteger value is divisible by the specified divisor.
Declaration
public static bool IsDivisibleBy(this BigInteger integer, BigInteger divisor)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | The BigInteger value to check. |
BigInteger | divisor | The divisor to check against. |
Returns
Type | Description |
---|---|
bool | true
|
IsOdd(BigInteger)
Indicates whether the specified BigInteger value is odd.
Declaration
public static bool IsOdd(this BigInteger integer)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | The BigInteger value to check. |
Returns
Type | Description |
---|---|
bool | true
|
IsPowerOf(BigInteger, int)
Indicates whether the specified BigInteger value is a power of the specified exponent.
Declaration
public static bool IsPowerOf(this BigInteger integer, int exponent)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | The BigInteger value to check. |
int | exponent | The exponent to check against. |
Returns
Type | Description |
---|---|
bool | true
|
LCM(BigInteger, BigInteger)
Returns the Least Common Multiple of two integers
Declaration
public static BigInteger LCM(this BigInteger first, BigInteger second)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | first | The first integer |
BigInteger | second | The second integer |
Returns
Type | Description |
---|---|
BigInteger | LCM of |
Examples
LCM(4, 6) = 12, since 12 is both a multiple of 4 (43) and a multiple of 6 (62)
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if |
Length(BigInteger, int)
Returns the number of coefficients (or digits) required to represent this
BigInteger in a specified base.
Declaration
public static int Length(this BigInteger integer, int base_)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | The BigInteger to calculate the coefficient count for. |
int | base_ | The base in which this BigInteger will be represented. Must be greater than |
Returns
Type | Description |
---|---|
int | The number of coefficients required to represent this BigInteger in base
|
Remarks
If this BigInteger is 0
, the result is always 0
, regardless of the base.
If this BigInteger is negative, the result will be the same as for its absolute value.
If the base base_
is less than 2
, this method returns 0
,
following the behavior of Log(BigInteger, double).
Mod(BigInteger, BigInteger)
Computes the modulus of a BigInteger in the same way as in languages like Python, Haskell, Julia and Matlab. The result follows the mathematical definition of modulus where the remainder always has the same sign as the divisor, ensuring predictable results for both positive and negative values.
Declaration
public static BigInteger Mod(this BigInteger integer, BigInteger modulus)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | The BigInteger value to compute the modulus for. |
BigInteger | modulus | The modulus value. Must be non-zero. |
Returns
Type | Description |
---|---|
BigInteger | The remainder when |
Remarks
The result of the modulus operation is adjusted based on the sign of modulus
:
When modulus
is positive, the result is in the range [0, modulus)
When modulus
is negative, the result is in the range (modulus, 0].
This method provides consistent behavior for modulus operations, similar to how it is implemented in Python, Haskell, Julia, and Matlab.
Examples
Examples:
BigInteger result1 = new BigInteger(10).Mod(3); // 1
BigInteger result2 = new BigInteger(-10).Mod(3); // 2
BigInteger result3 = new BigInteger(10).Mod(-3); // -2
BigInteger result4 = new BigInteger(-10).Mod(-3); // -1
Exceptions
Type | Condition |
---|---|
DivideByZeroException | Thrown when |
ModMinAbs(BigInteger, BigInteger)
Modulo operation yielding the remainder (positive or negative) with the smallest absolute value, preserving congruence under the specified modulus.
Declaration
public static BigInteger ModMinAbs(this BigInteger integer, BigInteger modulus)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | Dividend, an integer. |
BigInteger | modulus | Modulus, a positive integer. |
Returns
Type | Description |
---|---|
BigInteger | The integer |
Remarks
For both positive and negative integer
, the result minimizes |r|
, providing a "balanced" or symmetric result.
This is useful in contexts where closeness to zero is desired, such as balanced systems or modular
arithmetic with minimal magnitude deviation.
For example:
4 Mod 7
yields-3
instead of4
, as-3
is closer to zero.9 Mod 7
yields2
, as2
is already closest to zero.
ModularInverse(BigInteger, BigInteger)
Computes the modular inverse of a number integer
under a given modulus modulus
.
Declaration
public static BigInteger ModularInverse(this BigInteger integer, BigInteger modulus)
Parameters
Type | Name | Description |
---|---|---|
BigInteger | integer | The number for which the modular inverse is to be computed. Must be greater than or equal to 1. |
BigInteger | modulus | The modulus under which the inverse is computed. Must be greater than or equal to 2. |
Returns
Type | Description |
---|---|
BigInteger | The modular inverse of |
Remarks
The modular inverse of integer
modulo modulus
is a number x
such that:
integer ⋅ x ≡ 1 (mod modulus)
This method uses the Extended Euclidean Algorithm to compute the inverse.
The result of this method is always in the range [0, modulus
), as it is normalized by applying the modulus operation.
Examples
BigInteger result1 = new BigInteger(3).ModularInverse(7); // 5
BigInteger result2 = new BigInteger(2).ModularInverse(11); // 6
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | Thrown if |
ArgumentException | Thrown if |
Parse(string, int)
Parses the string representation of a number in the specified base to its BigInteger equivalent.
Declaration
public static BigInteger Parse(string input, int fromBase)
Parameters
Type | Name | Description |
---|---|---|
string | input | The string representation of the number to parse. It may start with a '-' to indicate a negative number. |
int | fromBase | The base of the input number, ranging from 2 to 36. |
Returns
Type | Description |
---|---|
BigInteger | A BigInteger representation of the parsed value. |
Examples
Console.WriteLine(Parse("-10.011", 2)); //outputs "-19/8"
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if |