gcd() public method

public gcd ( java arg0 ) : global::java.math.BigInteger
arg0 java
return global::java.math.BigInteger
Exemplo n.º 1
0
        /**
         * Reduces this rational number to the smallest numerator/denominator with the same value.
         *
         * @return the reduced rational number
         */
        public BigRational reduce()
        {
            BigInteger n = numerator.toBigInteger();
            BigInteger d = denominator.toBigInteger();

            BigInteger gcd = n.gcd(d);

            n = n.divide(gcd);
            d = d.divide(gcd);

            return(valueOf(n, d));
        }
Exemplo n.º 2
0
 public static object BIDivide(BigInteger n, BigInteger d)
 {
     if (d.Equals(BigIntegerZero))
         throw new ArithmeticException("Divide by zero");
     BigInteger gcd = n.gcd(d);
     if (gcd.Equals(BigIntegerZero))
         return 0;
     n = n.divide(gcd);
     d = d.divide(gcd);
     if (d.Equals(BigIntegerOne))
         return reduce(n);
     return new Ratio((d.signum() < 0 ? n.negate() : n),
         (d.signum() < 0 ? d.negate() : d));
 }
Exemplo n.º 3
0
        private static bool runMillerRabin(BigInteger number, SecureRandom random
			)
        {
            if (number.compareTo(BigInteger.valueOf(3)) <= 0)
            {
                return number.compareTo(BigInteger.ONE) != 0;
            }
            // Ensures that temp > 1 and temp < n.
            BigInteger temp = BigInteger.ZERO;
            do
            {
                temp = new BigInteger(number.bitLength() - 1, random);
            }
            while (temp.compareTo(BigInteger.ONE) <= 0);
            // Screen out n if our random number happens to share a factor with n.
            if (!number.gcd(temp).Equals(BigInteger.ONE))
            {
                return false;
            }
            // For debugging, prints out the integer to test with.
            //System.out.println("Testing with " + temp);
            BigInteger d = number.subtract(BigInteger.ONE);
            // Figure s and d Values
            int s = 0;
            while ((d.mod(TWO)).Equals(BigInteger.ZERO))
            {
                d = d.divide(TWO);
                s++;
            }
            BigInteger curValue = temp.modPow(d, number);
            // If this works out, it's a prime
            if (curValue.Equals(BigInteger.ONE))
            {
                return true;
            }
            // Otherwise, we will check to see if this value successively
            // squared ever yields -1.
            for (int r = 0; r < s; r++)
            {
                // We need to really check n-1 which is equivalent to -1.
                if (curValue.Equals(number.subtract(BigInteger.ONE)))
                {
                    return true;
                }
                else
                {
                    // Square this previous number - here I am just doubling the
                    // exponent. A more efficient implementation would store the
                    // value of the exponentiation and square it mod n.
                    curValue = curValue.modPow(TWO, number);
                }
            }
            // If none of our tests pass, we return false. The number is
            // definitively composite if we ever get here.
            return false;
        }