Java Code Examples for java.math.BigInteger#gcd()

The following examples show how to use java.math.BigInteger#gcd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EasyLinearEquation.java    From secretshare with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Original implementation.  Wrong.
 */
public static List<Trial> createList2(final BigInteger original,
                                     final BigInteger divideby,
                                     final BigInteger useModulus)
{
    List<Trial> list =  new ArrayList<Trial>();

    list.add(new Trial("original", original, divideby));
    list.add(new Trial("modoriginal", original.mod(useModulus), divideby));
    list.add(new Trial("moddivide", original, divideby.mod(useModulus)));
    list.add(new Trial("mod both", original.mod(useModulus), divideby.mod(useModulus)));

    BigInteger gcd = original.gcd(divideby);
    if ((gcd != null) &&
        (gcd.compareTo(BigInteger.ONE) > 0))
    {
        BigInteger divO = original.divide(gcd);
        BigInteger divD = divideby.divide(gcd);
        list.add(new Trial("gcd original", divO, divD));
        list.add(new Trial("gcd modoriginal", divO.mod(useModulus), divD));
        list.add(new Trial("gcd moddivide", divO, divD.mod(useModulus)));
        list.add(new Trial("gcd both", divO.mod(useModulus), divD.mod(useModulus)));
    }
    return list;
}
 
Example 2
Source File: BigFraction.java    From commons-numbers with Apache License 2.0 6 votes vote down vote up
/**
 * Private constructor: Instances are created using factory methods.
 *
 * <p>This constructor should only be invoked when the fraction is known
 * to be non-zero; otherwise use {@link #ZERO}. This avoids creating
 * the zero representation {@code 0 / -1}.
 *
 * @param num Numerator, must not be {@code null}.
 * @param den Denominator, must not be {@code null}.
 * @throws ArithmeticException if the denominator is zero.
 */
private BigFraction(BigInteger num, BigInteger den) {
    if (den.signum() == 0) {
        throw new FractionException(FractionException.ERROR_ZERO_DENOMINATOR);
    }

    // reduce numerator and denominator by greatest common denominator
    final BigInteger gcd = num.gcd(den);
    if (BigInteger.ONE.compareTo(gcd) < 0) {
        numerator = num.divide(gcd);
        denominator = den.divide(gcd);
    } else {
        numerator = num;
        denominator = den;
    }
}
 
Example 3
Source File: BigIntegerModPowTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * gcd: the first number is zero
 */
public void testGcdFirstZero() {
	byte aBytes[] = {0};
	byte bBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57};
	int aSign = 1;
	int bSign = 1;
	byte rBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57};
	BigInteger aNumber = new BigInteger(aSign, aBytes);
	BigInteger bNumber = new BigInteger(bSign, bBytes);
	BigInteger result = aNumber.gcd(bNumber);
	byte resBytes[] = new byte[rBytes.length];
	resBytes = result.toByteArray();
	for(int i = 0; i < resBytes.length; i++) {
		assertTrue(resBytes[i] == rBytes[i]);
	}
	assertEquals("incorrect sign", 1, result.signum());
}
 
Example 4
Source File: BigIntegerOperators.java    From totallylazy with Apache License 2.0 6 votes vote down vote up
public static Number divide(BigInteger n, BigInteger d) {
    if (d.equals(ZERO)) {
        throw new ArithmeticException("Divide by zero");
    }

    BigInteger gcd = n.gcd(d);

    if (gcd.equals(ZERO)) {
        return 0;
    }

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

    if (d.equals(BigInteger.ONE)) {
        return reduce(n);
    }

    if (d.equals(BigInteger.ONE.negate())) {
        return reduce(n.negate());
    }

    return new Ratio((d.signum() < 0 ? n.negate() : n), (d.signum() < 0 ? d.negate() : d));
}
 
Example 5
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws ZeroException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
    if (BigInteger.ZERO.equals(den)) {
        throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 6
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Create a {@link BigFraction} given the numerator and denominator as
 * <code>BigInteger</code>. The {@link BigFraction} is reduced to lowest terms.
 * </p>
 *
 * @param num
 *            the numerator, must not be <code>null</code>.
 * @param den
 *            the denominator, must not be <code>null</code>.
 * @throws ArithmeticException
 *             if the denominator is <code>zero</code>.
 * @throws NullPointerException
 *             if the numerator or the denominator is <code>zero</code>.
 */
public BigFraction(BigInteger num, BigInteger den) {
    if (num == null) {
        throw MathRuntimeException.createNullPointerException("numerator is null");
    }
    if (den == null) {
        throw MathRuntimeException.createNullPointerException("denominator is null");
    }
    if (BigInteger.ZERO.equals(den)) {
        throw MathRuntimeException.createArithmeticException(FORBIDDEN_ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 7
Source File: Math_36_BigFraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws ZeroException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
    if (BigInteger.ZERO.equals(den)) {
        throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 8
Source File: Math_36_BigFraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws ZeroException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
    if (BigInteger.ZERO.equals(den)) {
        throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 9
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Create a {@link BigFraction} given the numerator and denominator as
 * <code>BigInteger</code>. The {@link BigFraction} is reduced to lowest terms.
 * </p>
 *
 * @param num
 *            the numerator, must not be <code>null</code>.
 * @param den
 *            the denominator, must not be <code>null</code>.
 * @throws ArithmeticException
 *             if the denominator is <code>zero</code>.
 * @throws NullPointerException
 *             if the numerator or the denominator is <code>zero</code>.
 */
public BigFraction(BigInteger num, BigInteger den) {
    if (num == null) {
        throw MathRuntimeException.createNullPointerException("numerator is null");
    }
    if (den == null) {
        throw MathRuntimeException.createNullPointerException("denominator is null");
    }
    if (BigInteger.ZERO.equals(den)) {
        throw MathRuntimeException.createArithmeticException(FORBIDDEN_ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 10
Source File: BigFraction.java    From hipparchus with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws MathIllegalArgumentException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedCoreFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedCoreFormats.DENOMINATOR);
    if (den.signum() == 0) {
        throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_DENOMINATOR);
    }
    if (num.signum() == 0) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (den.signum() == -1) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 11
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}..
 * @throws ArithmeticException if the denominator is zero.
 */
public BigFraction(BigInteger num, BigInteger den) {
    if (num == null) {
        throw new NullArgumentException(LocalizedFormats.NUMERATOR);
    }
    if (den == null) {
        throw new NullArgumentException(LocalizedFormats.DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(den)) {
        throw MathRuntimeException.createArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 12
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Create a {@link BigFraction} given the numerator and denominator as
 * <code>BigInteger</code>. The {@link BigFraction} is reduced to lowest terms.
 * </p>
 *
 * @param num
 *            the numerator, must not be <code>null</code>.
 * @param den
 *            the denominator, must not be <code>null</code>.
 * @throws ArithmeticException
 *             if the denominator is <code>zero</code>.
 * @throws NullPointerException
 *             if the numerator or the denominator is <code>zero</code>.
 */
public BigFraction(BigInteger num, BigInteger den) {
    if (num == null) {
        throw MathRuntimeException.createNullPointerException("numerator is null");
    }
    if (den == null) {
        throw MathRuntimeException.createNullPointerException("denominator is null");
    }
    if (BigInteger.ZERO.equals(den)) {
        throw MathRuntimeException.createArithmeticException(FORBIDDEN_ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 13
Source File: BigFractionSym.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Compute the gcd of two rationals (this and other). The gcd is the rational number, such that dividing this and
 * other with the gcd will yield two co-prime integers.
 * 
 * @param other
 *            the second rational argument.
 * @return the gcd of this and other.
 */
public IFraction gcd(IFraction other) {
	if (other.isZero()) {
		return this;
	}
	BigInteger tdenom = this.toBigDenominator();
	BigInteger odenom = other.toBigDenominator();
	BigInteger gcddenom = tdenom.gcd(odenom);
	BigInteger denom = tdenom.divide(gcddenom).multiply(odenom);
	BigInteger num = toBigNumerator().gcd(other.toBigNumerator());
	return AbstractFractionSym.valueOf(num, denom);
}
 
Example 14
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws ArithmeticException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    if (num == null) {
        throw new NullArgumentException(LocalizedFormats.NUMERATOR);
    }
    if (den == null) {
        throw new NullArgumentException(LocalizedFormats.DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(den)) {
        throw MathRuntimeException.createArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 15
Source File: Order.java    From Qora with MIT License 5 votes vote down vote up
private BigDecimal calculateBuyIncrement(Order order, DBSet db)
{
	BigInteger multiplier = BigInteger.valueOf(100000000l);
	
	//CALCULATE THE MINIMUM INCREMENT AT WHICH I CAN BUY USING GCD
	BigInteger haveAmount = BigInteger.ONE.multiply(multiplier);
	BigInteger priceAmount = order.getPrice().multiply(new BigDecimal(multiplier)).toBigInteger();
	BigInteger gcd = haveAmount.gcd(priceAmount);
	haveAmount = haveAmount.divide(gcd);
	priceAmount = priceAmount.divide(gcd);
	
	//CALCULATE GCD IN COMBINATION WITH DIVISIBILITY
	if(this.getWantAsset(db).isDivisible())
	{
		haveAmount = haveAmount.multiply(multiplier);
	}
	if(this.getHaveAsset(db).isDivisible())
	{
		priceAmount = priceAmount.multiply(multiplier);
	}
	gcd = haveAmount.gcd(priceAmount);
	
	//CALCULATE THE INCREMENT AT WHICH WE HAVE TO BUY
	BigDecimal increment = new BigDecimal(haveAmount.divide(gcd));
	if(this.getWantAsset(db).isDivisible())
	{
		increment = increment.divide(new BigDecimal(multiplier));
	}
	
	//RETURN
	return increment;
}
 
Example 16
Source File: IntegerFunctions.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * Computation of the least common multiple of a set of BigIntegers.
 *
 * @param numbers - the set of numbers
 * @return the lcm(numbers)
 */
public static BigInteger leastCommonMultiple(BigInteger[] numbers)
{
    int n = numbers.length;
    BigInteger result = numbers[0];
    for (int i = 1; i < n; i++)
    {
        BigInteger gcd = result.gcd(numbers[i]);
        result = result.multiply(numbers[i]).divide(gcd);
    }
    return result;
}
 
Example 17
Source File: BigFraction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Create a {@link BigFraction} given the numerator and denominator as
 * <code>BigInteger</code>. The {@link BigFraction} is reduced to lowest terms.
 * </p>
 *
 * @param num
 *            the numerator, must not be <code>null</code>.
 * @param den
 *            the denominator, must not be <code>null</code>.
 * @throws ArithmeticException
 *             if the denominator is <code>zero</code>.
 * @throws NullPointerException
 *             if the numerator or the denominator is <code>zero</code>.
 */
public BigFraction(BigInteger num, BigInteger den) {
    if (num == null) {
        throw MathRuntimeException.createNullPointerException("numerator is null");
    }
    if (den == null) {
        throw MathRuntimeException.createNullPointerException("denominator is null");
    }
    if (BigInteger.ZERO.equals(den)) {
        throw MathRuntimeException.createArithmeticException("denominator must be different from 0");
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 18
Source File: BigFraction_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws ZeroException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
    if (BigInteger.ZERO.equals(den)) {
        throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 19
Source File: WeightedRouter.java    From sumk with Apache License 2.0 4 votes vote down vote up
private BigInteger gcd(BigInteger a, BigInteger b) {
	return a.gcd(b);
}
 
Example 20
Source File: BigIntegerOperators.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static BigInteger gcd(BigInteger bigInteger, BigInteger val) {
    return bigInteger.gcd(val);
}