Java Code Examples for com.google.common.math.LongMath#sqrt()

The following examples show how to use com.google.common.math.LongMath#sqrt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MathUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 开方
 */
public static long sqrt(long x, RoundingMode mode) {
	return LongMath.sqrt(x, mode);
}
 
Example 2
Source File: MathUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 开方
 */
public static long sqrt(long x, RoundingMode mode) {
	return LongMath.sqrt(x, mode);
}
 
Example 3
Source File: MathUtil.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
/**
 * 开方
 */
public static long sqrt(long x, RoundingMode mode) {
	return LongMath.sqrt(x, mode);
}
 
Example 4
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSqrtLongValues_shouldSqrtThemAndReturnTheResultForCeilingRounding() {
    long result = LongMath.sqrt(30L, RoundingMode.CEILING);
    assertEquals(6L, result);
}
 
Example 5
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSqrtLongValues_shouldSqrtThemAndReturnTheResultForFloorRounding() {
    long result = LongMath.sqrt(30L, RoundingMode.FLOOR);
    assertEquals(5L, result);
}
 
Example 6
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test(expected = ArithmeticException.class)
public void whenSqrtLongValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
    LongMath.sqrt(30L, RoundingMode.UNNECESSARY);
}
 
Example 7
Source File: Primality.java    From symja_android_library with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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 the n-th root which should be determined
 * @return <code>(result[0] ^ root ) * result[1]</code>
 */
public static long[] countRoot1021(final long val, int root) {
	long[] result = new long[2];
	result[1] = val;
	result[0] = 1L;

	long sqrt = LongMath.sqrt(val, RoundingMode.DOWN);
	long count = 0;
	long temp = val;
	// handle even values
	while ((temp & 0x00000001) == 0x00000000 && temp != 0L) {
		temp = temp >> 1L;
		count++;
		if (count == root) {
			count = 0;
			result[1] = result[1] >> root;
			result[0] = result[0] << 1L;
		}
	}

	for (int i = 1; i < primes.length; i++) {
		if (sqrt < primes[i]) {
			break;
		}
		count = 0;
		// divRem = temp.divideAndRemainder(BIprimes[i]);
		long[] divRem = new long[2];
		divRem[0] = temp / primes[i];
		divRem[1] = temp % primes[i];
		while (divRem[1] == 0L) {
			count++;
			if (count == root) {
				count = 0;
				// result[1] = result[1] / (primes[i].pow(root));
				result[1] = result[1] / pow(primes[i], root);
				result[0] = result[0] * primes[i];
			}
			temp = divRem[0];// quotient
			if (temp < primes[i]) {
				break;
			}
			// divRem = temp.divideAndRemainder(BIprimes[i]);
			divRem[0] = temp / primes[i];
			divRem[1] = temp % primes[i];
		}
	}
	return result;
}