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


Java BigIntegerMath.sqrt方法代码示例

本文整理汇总了Java中com.google.common.math.BigIntegerMath.sqrt方法的典型用法代码示例。如果您正苦于以下问题:Java BigIntegerMath.sqrt方法的具体用法?Java BigIntegerMath.sqrt怎么用?Java BigIntegerMath.sqrt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.common.math.BigIntegerMath的用法示例。


在下文中一共展示了BigIntegerMath.sqrt方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: countPrimes32749

import com.google.common.math.BigIntegerMath; //导入方法依赖的package包/类
/**
 * Factor the given value into primes less than <code>Short.MAX_VALUE</code> .
 * 
 * @param val
 *            a BigInteger value which should be factored by all primes less equal than 1021
 * @param map
 *            a map which counts the prime integer factors less equal than 32749
 * @return the rest factor or zero, if the number could be factored completely into primes less equal then 1021
 */
public static BigInteger countPrimes32749(final BigInteger val, Map<Integer, Integer> map) {
	BigInteger[] divRem;
	BigInteger result = val;
	BigInteger sqrt = BigIntegerMath.sqrt(result, RoundingMode.DOWN);
	Integer count;
	for (int i = 0; i < primes.length; i++) {
		if (sqrt.compareTo(BIprimes[i]) < 0) {
			break;
		}
		divRem = result.divideAndRemainder(BIprimes[i]);
		while (divRem[1].equals(BigInteger.ZERO)) {
			count = map.get(primes[i]);
			if (count == null) {
				map.put(primes[i], 1);
			} else {
				map.put(primes[i], count + 1);
			}
			result = divRem[0];// quotient
			sqrt = BigIntegerMath.sqrt(result, RoundingMode.DOWN);
			if (sqrt.compareTo(BIprimes[i]) < 0) {
				break;
			}
			divRem = result.divideAndRemainder(BIprimes[i]);
		}
	}
	BigInteger b;
	int prime;
	sqrt = BigIntegerMath.sqrt(result, RoundingMode.DOWN);
	for (int i = 0; i < SHORT_PRIMES.length; i++) {
		prime = SHORT_PRIMES[i];
		b = BigInteger.valueOf(prime);
		if (sqrt.compareTo(b) < 0) {
			break;
		}
		divRem = result.divideAndRemainder(b);
		while (divRem[1].equals(BigInteger.ZERO)) {
			count = map.get(prime);
			if (count == null) {
				map.put(prime, 1);
			} else {
				map.put(prime, count + 1);
			}
			result = divRem[0];// quotient
			sqrt = BigIntegerMath.sqrt(result, RoundingMode.DOWN);
			if (sqrt.compareTo(b) < 0) {
				break;
			}
			divRem = result.divideAndRemainder(b);
		}
	}
	return result;
}
 
开发者ID:axkr,项目名称:symja_android_library,代码行数:62,代码来源:Primality.java

示例2: countRoot1021

import com.google.common.math.BigIntegerMath; //导入方法依赖的package包/类
/**
 * Determine the n-th root from the prime decomposition of the primes[] array.
 * 
 * @param val
 *            a BigInteger value which should be factored by all primes less equal than 1021
 * @param root
 *            th n-th root which should be determined
 * @return <code>(result[0] ^ root ) * result[1]</code>
 */
public static BigInteger[] countRoot1021(final BigInteger val, int root) {
	BigInteger[] result = new BigInteger[2];
	result[1] = val;
	result[0] = BigInteger.ONE;
	BigInteger[] divRem;
	BigInteger sqrt = BigIntegerMath.sqrt(val, RoundingMode.DOWN);
	int count = 0;
	BigInteger temp = val;
	// handle even values
	while (temp.and(BigInteger.ONE).equals(BigInteger.ZERO) && !temp.equals(BigInteger.ZERO)) {
		temp = temp.shiftRight(1);
		count++;
		if (count == root) {
			count = 0;
			result[1] = result[1].shiftRight(root);
			result[0] = result[0].shiftLeft(1);
		}
	}

	for (int i = 1; i < primes.length; i++) {
		if (sqrt.compareTo(BIprimes[i]) < 0) {
			break;
		}
		count = 0;
		divRem = temp.divideAndRemainder(BIprimes[i]);
		while (divRem[1].equals(BigInteger.ZERO)) {
			count++;
			if (count == root) {
				count = 0;
				result[1] = result[1].divide(BIprimes[i].pow(root));
				result[0] = result[0].multiply(BIprimes[i]);
			}
			temp = divRem[0];// quotient
			if (temp.compareTo(BIprimes[i]) < 0) {
				break;
			}
			divRem = temp.divideAndRemainder(BIprimes[i]);
		}
	}
	return result;
}
 
开发者ID:axkr,项目名称:symja_android_library,代码行数:51,代码来源:Primality.java


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