Jurassic.BigInteger.MultiplyAdd C# (CSharp) Method

MultiplyAdd() public static method

Multiply by m and add a.
public static MultiplyAdd ( BigInteger b, int m, int a ) : BigInteger
b BigInteger
m int
a int
return BigInteger
        public static BigInteger MultiplyAdd(BigInteger b, int m, int a)
        {
            if (m <= 0)
                throw new ArgumentOutOfRangeException("m");
            if (a < 0)
                throw new ArgumentOutOfRangeException("a");
            if (b.sign == 0)
                return new BigInteger(a);
            uint[] outputBits = new uint[b.wordCount + 1];
            int outputWordCount = b.wordCount;
            uint carry = (uint)a;
            for (int i = 0; i < b.wordCount; i++)
            {
                ulong temp = b.bits[i] * (ulong)m + carry;
                carry = (uint)(temp >> 32);
                outputBits[i] = (uint)temp;
            }
            if (carry != 0)
            {
                outputBits[outputWordCount] = carry;
                outputWordCount++;
            }
            return new BigInteger(outputBits, outputWordCount, 1);
        }

Usage Example

示例#1
0
        /// <summary>
        /// Equivalent to BigInteger.Pow but with integer arguments.
        /// </summary>
        /// <param name="radix"> The number to be raised to a power. </param>
        /// <param name="exponent"> The number that specifies the power. </param>
        /// <returns> The number <paramref name="radix"/> raised to the power
        /// <paramref name="exponent"/>. </returns>
        public static BigInteger Pow(int radix, int exponent)
        {
            if (radix < 0 || radix > 36)
            {
                throw new ArgumentOutOfRangeException("radix");
            }
            if (exponent < 0)
            {
                throw new ArgumentOutOfRangeException("exponent");
            }

            if (radix == 10 && exponent < integerPowersOfTen.Length)
            {
                // Use a table for quick lookup of powers of 10.
                return(new BigInteger(integerPowersOfTen[exponent]));
            }
            else if (radix == 2)
            {
                // Power of two is easy.
                return(BigInteger.LeftShift(BigInteger.One, exponent));
            }

            // Special cases.
            switch (exponent)
            {
            case 0:
                return(BigInteger.One);

            case 1:
                return(new BigInteger(radix));

            case 2:
                return(new BigInteger(radix * radix));

            case 3:
                return(new BigInteger(radix * radix * radix));
            }

            // Use recursion to calculate the result.
            if ((exponent & 1) == 1)
            {
                // Exponent is odd.
                var temp = Pow(radix, exponent / 2);
                return(BigInteger.MultiplyAdd(BigInteger.Multiply(temp, temp), radix, 0));
            }
            else
            {
                // Exponent is even.
                var temp = Pow(radix, exponent / 2);
                return(BigInteger.Multiply(temp, temp));
            }
        }
All Usage Examples Of Jurassic.BigInteger::MultiplyAdd