本文整理汇总了Java中java.math.BigInteger.gcd方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.gcd方法的具体用法?Java BigInteger.gcd怎么用?Java BigInteger.gcd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.gcd方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* This method is the core of all constructors. It takes the numerator and
* the denominator as BigIntegers and produces a BigRational, reducing the
* fraction if needed.
*
* @param numerator The numerator of the rational number
* @param denominator The denominator of the rational number
* @param isNormal True if the fraction is already in reduced form, false otherwise.
*/
private void init(BigInteger numerator, BigInteger denominator, boolean isNormal) {
// If isNormal, then we do not need to reduce the fraction and compute the gcd
// deal with x / 0
if (denominator.equals(BigInteger.ZERO)) {
throw new RuntimeException("Denominator is zero");
}
if (!isNormal) {
// reduce fraction
BigInteger g = numerator.gcd(denominator);
num = numerator.divide(g);
den = denominator.divide(g);
} else {
num = numerator;
den = denominator;
}
}
示例2: Fraction
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Creates a new Fraction of the form (numerator/denominator) and divides both
* of them by their common greatest divisor to keep the numbers as short as
* possible.
* @param numerator the numerator of the fraction
* @param denominator the denominator of the fraction
*/
public Fraction(BigInteger numerator, BigInteger denominator){
if (numerator.equals(BigInteger.ZERO)){
this.numerator = BigInteger.ZERO;
this.denominator = BigInteger.ONE;
} else {
BigInteger gcd = numerator.gcd(denominator);
if (gcd.equals(BigInteger.ONE)){
this.numerator = numerator;
this.denominator = denominator;
} else {
this.numerator = numerator.divide(gcd);
this.denominator = denominator.divide(gcd);
}
if (this.denominator.signum() == -1){
this.numerator = this.numerator.negate();
this.denominator = this.denominator.negate();
}
}
int shift = this.denominator.bitLength() - maxBitLength;
if (shift > 0){
this.denominator = this.denominator.shiftRight(shift);
this.numerator = this.numerator.shiftRight(shift);
}
}
示例3: leastCommonMultiple
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* 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;
}
示例4: lcm
import java.math.BigInteger; //导入方法依赖的package包/类
private long lcm(final long m, final long n) {
if (m == 0 && n == 0) {
return 0;
}
final BigInteger mi = BigInteger.valueOf(m);
final BigInteger ni = BigInteger.valueOf(n);
final BigInteger gcd = mi.gcd(ni);
final BigInteger r = mi.multiply(ni).divide(gcd);
return r.longValue();
}
示例5: lcm
import java.math.BigInteger; //导入方法依赖的package包/类
/** The lowest common multiple
* @param a The first argument
* @param b The second argument
* @return lcm(|a|,|b|)
* @since 2010-08-27
* @author Richard J. Mathar
*/
static public BigInteger lcm(final BigInteger a, final BigInteger b)
{
BigInteger g = a.gcd(b) ;
return a.multiply(b).abs().divide(g) ;
}