本文整理了Java中java.math.BigInteger.gcd()
方法的一些代码示例,展示了BigInteger.gcd()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.gcd()
方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:gcd
[英]Returns a BigInteger whose value is greatest common divisor of this and value. If this == 0 and value == 0 then zero is returned, otherwise the result is positive.
[中]返回一个BigInteger,其值是该和值的最大公约数。如果此==0且值==0,则返回零,否则结果为正。
代码示例来源:origin: stackoverflow.com
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();
}
代码示例来源:origin: stackoverflow.com
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();
}
代码示例来源:origin: deeplearning4j/nd4j
/**
* 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 */
代码示例来源:origin: org.apache.commons/commons-math3
/**
* <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;
}
}
代码示例来源:origin: google/guava
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)));
}
}
}
代码示例来源:origin: google/guava
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)));
}
}
}
代码示例来源:origin: org.apache.commons/commons-math3
final BigInteger gcd = num.gcd(den);
if (BigInteger.ONE.compareTo(gcd) < 0) {
num = num.divide(gcd);
代码示例来源:origin: robovm/robovm
gcd = p.gcd(q);
p = p.divide(gcd);
q = q.divide(gcd);
代码示例来源:origin: stackoverflow.com
BigInteger gcd = numerator.gcd(denominator);
if (!gcd.equals(BigInteger.ONE)) {
numerator = numerator.divide(gcd);
代码示例来源:origin: stackoverflow.com
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);
代码示例来源:origin: i2p/i2p.i2p
ok = k.compareTo(pm1) == -1;
ok = ok && k.compareTo(BigInteger.ONE) == 1;
ok = ok && k.gcd(pm1).equals(BigInteger.ONE);
} while (!ok);
代码示例来源:origin: com.madgag.spongycastle/core
/**
* 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();
}
代码示例来源:origin: org.solovyev/jscl
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)));
}
代码示例来源:origin: org.apache.commons/commons-math
/**
* <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));
}
代码示例来源:origin: org.apache.commons/math
/**
* <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));
}
代码示例来源:origin: org.solovyev/jscl
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();
}
代码示例来源:origin: tec.uom/uom-se
@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);
}
代码示例来源:origin: tec.units/indriya
@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);
}
代码示例来源:origin: com.google.guava/guava-tests
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)));
}
}
}
代码示例来源:origin: org.jruby/jruby-complete
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);
}
内容来源于网络,如有侵权,请联系作者删除!