当前位置: 首页>>代码示例>>Java>>正文


Java BigInteger.gcd方法代码示例

本文整理汇总了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;
    }
}
 
开发者ID:max6cn,项目名称:jmt,代码行数:28,代码来源:BigRational.java

示例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);
}
   }
 
开发者ID:wwu-pi,项目名称:tap17-muggl-javaee,代码行数:32,代码来源:Fraction.java

示例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;
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:18,代码来源:IntegerFunctions.java

示例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();
}
 
开发者ID:polarsys,项目名称:time4sys,代码行数:11,代码来源:LongDurationImpl.java

示例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) ;
}
 
开发者ID:Baizey,项目名称:Helpers,代码行数:13,代码来源:BigIntegerMath.java


注:本文中的java.math.BigInteger.gcd方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。