当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.compareTo方法代码示例

本文整理汇总了C#中BigInteger.compareTo方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.compareTo方法的具体用法?C# BigInteger.compareTo怎么用?C# BigInteger.compareTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BigInteger的用法示例。


在下文中一共展示了BigInteger.compareTo方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: completeInPlaceSubtract

 /**
  * Same as
  *
  * @link #inplaceSubtract(BigInteger, BigInteger), but without the
  *       restriction of non-positive values
  * @param op1
  *            should have enough space to save the result
  * @param op2
  */
 internal static void completeInPlaceSubtract(BigInteger op1, BigInteger op2)
 {
     int resultSign = op1.compareTo (op2);
     if (op1.sign == 0) {
         java.lang.SystemJ.arraycopy (op2.digits, 0, op1.digits, 0, op2.numberLength);
         op1.sign = -op2.sign;
     } else if (op1.sign != op2.sign) {
         add (op1.digits, op1.digits, op1.numberLength, op2.digits,
             op2.numberLength);
         op1.sign = resultSign;
     } else {
         int sign = unsignedArraysCompare (op1.digits,
                 op2.digits, op1.numberLength, op2.numberLength);
         if (sign > 0) {
             subtract (op1.digits, op1.digits, op1.numberLength, op2.digits,
                     op2.numberLength);	// op1 = op1 - op2
             // op1.sign remains equal
         } else {
             inverseSubtract (op1.digits, op1.digits, op1.numberLength,
                     op2.digits, op2.numberLength);	// op1 = op2 - op1
             op1.sign = -op1.sign;
         }
     }
     op1.numberLength = java.lang.Math.max (op1.numberLength, op2.numberLength) + 1;
     op1.cutOffLeadingZeroes ();
     op1.unCache();
 }
开发者ID:gadfly,项目名称:nofs,代码行数:36,代码来源:java.math.Elementary.cs

示例2: modInverseMontgomery

        /**
         * Calculates a.modInverse(p) Based on: Savas, E; Koc, C "The Montgomery Modular
         * Inverse - Revised"
         */
        internal static BigInteger modInverseMontgomery(BigInteger a, BigInteger p)
        {
            if (a.sign == 0){
                // ZERO hasn't inverse
                // math.19: BigInteger not invertible
                throw new ArithmeticException("BigInteger not invertible");
                }

            if (!p.testBit(0)){
                // montgomery inverse require even modulo
                return modInverseHars(a, p);
            }

            int m = p.numberLength * 32;
            // PRE: a \in [1, p - 1]
            BigInteger u, v, r, s;
            u = p.copy();  // make copy to use inplace method
            v = a.copy();
            int max = java.lang.Math.max(v.numberLength, u.numberLength);
            r = new BigInteger(1, 1, new int[max + 1]);
            s = new BigInteger(1, 1, new int[max + 1]);
            s.digits[0] = 1;
            // s == 1 && v == 0

            int k = 0;

            int lsbu = u.getLowestSetBit();
            int lsbv = v.getLowestSetBit();
            int toShift;

            if (lsbu > lsbv) {
                BitLevel.inplaceShiftRight(u, lsbu);
                BitLevel.inplaceShiftRight(v, lsbv);
                BitLevel.inplaceShiftLeft(r, lsbv);
                k += lsbu - lsbv;
            } else {
                BitLevel.inplaceShiftRight(u, lsbu);
                BitLevel.inplaceShiftRight(v, lsbv);
                BitLevel.inplaceShiftLeft(s, lsbu);
                k += lsbv - lsbu;
            }

            r.sign = 1;
            while (v.signum() > 0) {
                // INV v >= 0, u >= 0, v odd, u odd (except last iteration when v is even (0))

                while (u.compareTo(v) > BigInteger.EQUALS) {
                    Elementary.inplaceSubtract(u, v);
                    toShift = u.getLowestSetBit();
                    BitLevel.inplaceShiftRight(u, toShift);
                    Elementary.inplaceAdd(r, s);
                    BitLevel.inplaceShiftLeft(s, toShift);
                    k += toShift;
                }

                while (u.compareTo(v) <= BigInteger.EQUALS) {
                    Elementary.inplaceSubtract(v, u);
                    if (v.signum() == 0)
                        break;
                    toShift = v.getLowestSetBit();
                    BitLevel.inplaceShiftRight(v, toShift);
                    Elementary.inplaceAdd(s, r);
                    BitLevel.inplaceShiftLeft(r, toShift);
                    k += toShift;
                }
            }
            if (!u.isOne()){
                // in u is stored the gcd
                // math.19: BigInteger not invertible.
                throw new ArithmeticException("BigInteger not invertible");
            }
            if (r.compareTo(p) >= BigInteger.EQUALS) {
                Elementary.inplaceSubtract(r, p);
            }

            r = p.subtract(r);

            // Have pair: ((BigInteger)r, (Integer)k) where r == a^(-1) * 2^k mod (module)
            int n1 = calcN(p);
            if (k > m) {
                r = monPro(r, BigInteger.ONE, p, n1);
                k = k - m;
            }

            r = monPro(r, BigInteger.getPowerOfTwo(m - k), p, n1);
            return r;
        }
开发者ID:sailesh341,项目名称:JavApi,代码行数:91,代码来源:Division.cs

示例3: gcdBinary

        /**
         * @param m a positive modulus
         * Return the greatest common divisor of op1 and op2,
         *
         * @param op1
         *            must be greater than zero
         * @param op2
         *            must be greater than zero
         * @see BigInteger#gcd(BigInteger)
         * @return {@code GCD(op1, op2)}
         */
        internal static BigInteger gcdBinary(BigInteger op1, BigInteger op2)
        {
            // PRE: (op1 > 0) and (op2 > 0)

            /*
             * Divide both number the maximal possible times by 2 without rounding
                     * gcd(2*a, 2*b) = 2 * gcd(a,b)
             */
            int lsb1 = op1.getLowestSetBit();
            int lsb2 = op2.getLowestSetBit();
            int pow2Count = java.lang.Math.min(lsb1, lsb2);

                BitLevel.inplaceShiftRight(op1, lsb1);
                BitLevel.inplaceShiftRight(op2, lsb2);

            BigInteger swap;
            // I want op2 > op1
            if (op1.compareTo(op2) == BigInteger.GREATER) {
                swap = op1;
                op1 = op2;
                op2 = swap;
            }

            do { // INV: op2 >= op1 && both are odd unless op1 = 0

                // Optimization for small operands
                // (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64)
                if (( op2.numberLength == 1 )
                || ( ( op2.numberLength == 2 ) && ( op2.digits[1] > 0 ) )) {
                    op2 = BigInteger.valueOf(Division.gcdBinary(op1.longValue(),
                            op2.longValue()));
                    break;
            }

                // Implements one step of the Euclidean algorithm
                // To reduce one operand if it's much smaller than the other one
                if (op2.numberLength > op1.numberLength * 1.2) {
                    op2 = op2.remainder(op1);
                    if (op2.signum() != 0) {
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit());
                }
                } else {

                    // Use Knuth's algorithm of successive subtract and shifting
                    do {
                        Elementary.inplaceSubtract(op2, op1); // both are odd
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit()); // op2 is even
                    } while (op2.compareTo(op1) >= BigInteger.EQUALS);
                }
                // now op1 >= op2
                swap = op2;
                op2 = op1;
                op1 = swap;
            } while (op1.sign != 0);
            return op2.shiftLeft(pow2Count);
        }
开发者ID:sailesh341,项目名称:JavApi,代码行数:67,代码来源:Division.cs

示例4: modInverseHars

        /**
         * Implements the "Shifting Euclidean modular inverse algorithm".
         * "Laszlo Hars - Modular Inverse Algorithms Without Multiplications
         * for Cryptographic Applications"
         *
         * @see BigInteger#modInverse(BigInteger)
         * @param a
         *            a positive number
         * @param m
         *            a positive modulus
         */
        internal static BigInteger modInverseHars(BigInteger a, BigInteger m)
        {
            // PRE: (a > 0) and (m > 0)
            BigInteger u, v, r, s, temp;
            // u = MAX(a,m), v = MIN(a,m)
            if (a.compareTo(m) == BigInteger.LESS) {
                u = m;
                v = a;
                r = BigInteger.ZERO;
                s = BigInteger.ONE;
            } else {
                v = m;
                u = a;
                s = BigInteger.ZERO;
                r = BigInteger.ONE;
            }
            int uLen = u.bitLength();
            int vLen = v.bitLength();
            int f = uLen - vLen;

            while (vLen > 1) {
                if (u.sign == v.sign) {
                    u = u.subtract(v.shiftLeft(f));
                    r = r.subtract(s.shiftLeft(f));
                } else {
                    u = u.add(v.shiftLeft(f));
                    r = r.add(s.shiftLeft(f));
                }
                uLen = u.abs().bitLength();
                vLen = v.abs().bitLength();
                f = uLen - vLen;
                if (f < 0) {
                    // SWAP(u,v)
                    temp = u;
                    u = v;
                    v = temp;
                    // SWAP(r,s)
                    temp = r;
                    r = s;
                    s = temp;

                    f = -f;
                    vLen = uLen;
                }
            }
            if (v.sign == 0) {
                return BigInteger.ZERO;
            }
            if (v.sign < 0) {
                s = s.negate();
            }
            if (s.compareTo(m) == BigInteger.GREATER) {
                return s.subtract(m);
            }
            if (s.sign < 0) {
                return s.add(m);
            }
            return s; // a^(-1) mod m
        }
开发者ID:sailesh341,项目名称:JavApi,代码行数:70,代码来源:Division.cs

示例5: millerRabin

        /**
         * The Miller-Rabin primality test.
         *
         * @param n the input number to be tested.
         * @param t the number of trials.
         * @return {@code false} if the number is definitely compose, otherwise
         *         {@code true} with probability {@code 1 - 4<sup>(-t)</sup>}.
         * @ar.org.fitc.ref "D. Knuth, The Art of Computer Programming Vo.2, Section
         *                  4.5.4., Algorithm P"
         */
        private static bool millerRabin(BigInteger n, int t)
        {
            // PRE: n >= 0, t >= 0
            BigInteger x; // x := UNIFORM{2...n-1}
            BigInteger y; // y := x^(q * 2^j) mod n
            BigInteger n_minus_1 = n.subtract(BigInteger.ONE); // n-1
            int bitLength = n_minus_1.bitLength(); // ~ log2(n-1)
            // (q,k) such that: n-1 = q * 2^k and q is odd
            int k = n_minus_1.getLowestSetBit();
            BigInteger q = n_minus_1.shiftRight(k);
            java.util.Random rnd = new java.util.Random();

            for (int i = 0; i < t; i++) {
                // To generate a witness 'x', first it use the primes of table
                if (i < primes.Length) {
                    x = BIprimes[i];
                } else {/*
                         * It generates random witness only if it's necesssary. Note
                         * that all methods would call Miller-Rabin with t <= 50 so
                         * this part is only to do more robust the algorithm
                         */
                    do {
                        x = new BigInteger(bitLength, rnd);
                    } while ((x.compareTo(n) >= BigInteger.EQUALS) || (x.sign == 0)
                            || x.isOne());
                }
                y = x.modPow(q, n);
                if (y.isOne() || y.equals(n_minus_1)) {
                    continue;
                }
                for (int j = 1; j < k; j++) {
                    if (y.equals(n_minus_1)) {
                        continue;
                    }
                    y = y.multiply(y).mod(n);
                    if (y.isOne()) {
                        return false;
                    }
                }
                if (!y.equals(n_minus_1)) {
                    return false;
                }
            }
            return true;
        }
开发者ID:sailesh341,项目名称:JavApi,代码行数:55,代码来源:Primality.cs


注:本文中的BigInteger.compareTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。