MathLib Docs MathLib Docs
MathLib Docs MathLib Docs

Search Results for

    Edit this page

    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.

    Inheritance
    object
    BigIntegerExtensions
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: MathLib
    Assembly: MathLib.dll
    Syntax
    public static class BigIntegerExtensions

    Methods

    | Edit this page View Source

    Abs(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.

    | Edit this page View Source

    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 iff a and b are coprime.

    | Edit this page View Source

    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:

    • The greatest common divisor (gcd) of a and b.
    • The Bézout coefficient 'x' (corresponding to a in Bézout's identity).
    • The Bézout coefficient 'y' (corresponding to b in Bézout's identity).
    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)
    | Edit this page View Source

    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 iff the value is divisible by the divisor.

    | Edit this page View Source

    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 iff integer is odd.

    | Edit this page View Source

    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 iff the value is a power of the exponent.

    | Edit this page View Source

    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 first and second

    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 first or second is zero.

    | Edit this page View Source

    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 1 for meaningful results.

    Returns
    Type Description
    int

    The number of coefficients required to represent this BigInteger in base base_.

    0 if base_ is less than 2.

    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).

    | Edit this page View Source

    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 integer is divided by modulus, adjusted to follow the same sign as modulus.

    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 modulus is zero.

    | Edit this page View Source

    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 r closest to zero for which there exists an integer k such that n = k × mod + r, and -mod/2 ≤ r ≤ mod/2.

    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 of 4, as -3 is closer to zero.
    • 9 Mod 7 yields 2, as 2 is already closest to zero.
    | Edit this page View Source

    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 integer modulo modulus. The result is normalized to be within the range [0, modulus).

    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 integer is less than 1 or if modulus is less than 2.

    ArgumentException

    Thrown if integer and modulus are not coprime, i.e., if the greatest common divisor of the two is not 1.

    | Edit this page View Source

    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 fromBase is not within 2 to 36.