Exemplo n.º 1
0
        /// <summary>
        /// Performs modulus inversion on an Integer.
        /// </summary>
        /// <param name="value">The Integer to inverse.</param>
        /// <param name="modulus">The Integer by which to divide.</param>
        /// <returns>The result of inversing the Integer; if any.</returns>
        public static Integer ModInv(this Integer value, Integer modulus)
        {
            if (modulus < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(modulus));
            }

            if (Abs(value) >= modulus)
            {
                value = value % modulus;
            }

            if (value < 0)
            {
                value = value + modulus;
                var result = Lehmer.Inv(modulus, value);
                if (result != 0)
                {
                    result = result - modulus;
                }
                return(result);
            }

            return(Lehmer.Inv(modulus, value));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finds the greatest common divisor of two Integers.
        /// </summary>
        /// <param name="left">The first value.</param>
        /// <param name="right">The second value.</param>
        /// <returns>The greatest common divisor of left and right.</returns>
        public static Integer Gcd(this Integer left, Integer right)
        {
            var a = Abs(left);
            var b = Abs(right);

            if (a < b)
            {
                return(Lehmer.Gcd(b, a));
            }
            return(Lehmer.Gcd(a, b));
        }