Exemplo n.º 1
0
        public BigInteger ModPow(int power, BigInteger mod)
        {
            if (power == 0)
            {
                return(One);
            }
            if (power < 0)
            {
                throw new ArgumentOutOfRangeException("NonNegativePower");
            }
            BigInteger factor = this;
            BigInteger result = One; //!!! want a mutable here for efficiency

            while (power != 0)
            {
                if ((power & 1) != 0)
                {
                    result = result * factor;
                    result = result % mod; //!!! should do all in one step
                }
                factor  = factor.Square();
                power >>= 1;
            }
            return(result);
        }
Exemplo n.º 2
0
        public BigInteger Power(int exp)
        {
            if (exp == 0)
            {
                return(One);
            }
            if (exp < 0)
            {
                throw new ArgumentOutOfRangeException("NonNegativePower");
            }
            BigInteger factor = this;
            BigInteger result = One; //!!! want a mutable here for efficiency

            while (exp != 0)
            {
                if ((exp & 1) != 0)
                {
                    result = result * factor;
                }
                if (exp == 1)
                {
                    break;            // avoid costly factor.square()
                }
                factor = factor.Square();
                exp  >>= 1;
            }
            return(result);
        }