private static int gcdThing(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); }
private static int gcdThing(int a, int b) { BigInteger b1 = new BigInteger(""+a); // there's a better way to do this. I forget. BigInteger b2 = new BigInteger(""+b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); }
/** * Normalize to coprime numerator and denominator. * Also copy a negative sign of the denominator to the numerator. */ protected void normalize() { /* compute greatest common divisor of numerator and denominator */ final BigInteger g = a.gcd(b); if (g.compareTo(BigInteger.ONE) > 0) { a = a.divide(g); b = b.divide(g); } if (b.compareTo(BigInteger.ZERO) == -1) { a = a.negate(); b = b.negate(); } } /* Rational.normalize */
/** * <p> * Reduce this <code>BigFraction</code> to its lowest terms. * </p> * * @return the reduced <code>BigFraction</code>. It doesn't change anything if * the fraction can be reduced. */ public BigFraction reduce() { final BigInteger gcd = numerator.gcd(denominator); if (BigInteger.ONE.compareTo(gcd) < 0) { return new BigFraction(numerator.divide(gcd), denominator.divide(gcd)); } else { return this; } }
public void testGCD() { for (int a : POSITIVE_INTEGER_CANDIDATES) { for (int b : POSITIVE_INTEGER_CANDIDATES) { assertEquals(valueOf(a).gcd(valueOf(b)), valueOf(IntMath.gcd(a, b))); } } }
public void testGCDExhaustive() { for (long a : POSITIVE_LONG_CANDIDATES) { for (long b : POSITIVE_LONG_CANDIDATES) { assertEquals(valueOf(a).gcd(valueOf(b)), valueOf(LongMath.gcd(a, b))); } } }
final BigInteger gcd = num.gcd(den); if (BigInteger.ONE.compareTo(gcd) < 0) { num = num.divide(gcd);
gcd = p.gcd(q); p = p.divide(gcd); q = q.divide(gcd);
BigInteger gcd = numerator.gcd(denominator); if (!gcd.equals(BigInteger.ONE)) { numerator = numerator.divide(gcd);
BigInteger gcd = numerator.gcd(denominator); this.numerator = numerator.divide(gcd); this.denominator = denominator.divide(gcd); tmpNumerator = tmpNumerator.multiply(BigInteger.valueOf(0x10000000000000L + mantissa)); BigInteger gcd = tmpNumerator.gcd(tmpDenominator); numerator = tmpNumerator.divide(gcd); denominator = tmpDenominator.divide(gcd);
ok = k.compareTo(pm1) == -1; ok = ok && k.compareTo(BigInteger.ONE) == 1; ok = ok && k.gcd(pm1).equals(BigInteger.ONE); } while (!ok);
/** * Computes the greatest common divisor of the two specified integers * * @param u - first integer * @param v - second integer * @return gcd(a, b) */ public static int gcd(int u, int v) { return BigInteger.valueOf(u).gcd(BigInteger.valueOf(v)).intValue(); }
public Rational multiply(Rational rational) { BigInteger gcd = numerator.gcd(rational.denominator); BigInteger gcd2 = denominator.gcd(rational.numerator); return new Rational(numerator.divide(gcd).multiply(rational.numerator.divide(gcd2)), denominator.divide(gcd2).multiply(rational.denominator.divide(gcd))); }
/** * <p> * Reduce this <code>BigFraction</code> to its lowest terms. * </p> * * @return the reduced <code>BigFraction</code>. It doesn't change anything if * the fraction can be reduced. */ public BigFraction reduce() { final BigInteger gcd = numerator.gcd(denominator); return new BigFraction(numerator.divide(gcd), denominator.divide(gcd)); }
/** * <p> * Reduce this <code>BigFraction</code> to its lowest terms. * </p> * * @return the reduced <code>BigFraction</code>. It doesn't change anything if * the fraction can be reduced. */ public BigFraction reduce() { final BigInteger gcd = numerator.gcd(denominator); return new BigFraction(numerator.divide(gcd), denominator.divide(gcd)); }
public Rational add(Rational rational) { BigInteger gcd = denominator.gcd(rational.denominator); BigInteger c = denominator.divide(gcd); BigInteger c2 = rational.denominator.divide(gcd); return new Rational(numerator.multiply(c2).add(rational.numerator.multiply(c)), denominator.multiply(c2)).reduce(); }
@Override public UnitConverter concatenate(UnitConverter converter) { if (!(converter instanceof RationalConverter)) return super.concatenate(converter); RationalConverter that = (RationalConverter) converter; BigInteger newDividend = this.getDividend().multiply(that.getDividend()); BigInteger newDivisor = this.getDivisor().multiply(that.getDivisor()); BigInteger gcd = newDividend.gcd(newDivisor); newDividend = newDividend.divide(gcd); newDivisor = newDivisor.divide(gcd); return (newDividend.equals(BigInteger.ONE) && newDivisor.equals(BigInteger.ONE)) ? IDENTITY : new RationalConverter(newDividend, newDivisor); }
@Override public UnitConverter concatenate(UnitConverter converter) { if (!(converter instanceof RationalConverter)) return super.concatenate(converter); RationalConverter that = (RationalConverter) converter; BigInteger newDividend = this.getDividend().multiply(that.getDividend()); BigInteger newDivisor = this.getDivisor().multiply(that.getDivisor()); BigInteger gcd = newDividend.gcd(newDivisor); newDividend = newDividend.divide(gcd); newDivisor = newDivisor.divide(gcd); return (newDividend.equals(BigInteger.ONE) && newDivisor.equals(BigInteger.ONE)) ? IDENTITY : new RationalConverter(newDividend, newDivisor); }
public void testGCD() { for (int a : POSITIVE_INTEGER_CANDIDATES) { for (int b : POSITIVE_INTEGER_CANDIDATES) { assertEquals(valueOf(a).gcd(valueOf(b)), valueOf(IntMath.gcd(a, b))); } } }
public static RubyInteger f_gcd(ThreadContext context, RubyInteger x, RubyInteger y) { if (x instanceof RubyFixnum && y instanceof RubyFixnum && isLongMinValue((RubyFixnum) x)) return RubyFixnum.newFixnum(context.runtime, i_gcd(x.getLongValue(), y.getLongValue())); BigInteger gcd = x.getBigIntegerValue().gcd(y.getBigIntegerValue()); if (gcd.compareTo(RubyBignum.LONG_MAX) <= 0) { // gcd always positive return RubyFixnum.newFixnum(context.runtime, gcd.longValue()); } return RubyBignum.newBignum(context.runtime, gcd); }