Android Utililty Methods Int GCD

List of utility methods to do Int GCD

Description

The list of methods to do Int GCD are organized into topic(s).

Method

intgcd(int p, int q)
Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method.
int a = p;
int b = q;
if (a == 0 || b == 0) {
    if (a == Integer.MIN_VALUE || b == Integer.MIN_VALUE) {
        throw new MathArithmeticException(
                LocalizedFormats.GCD_OVERFLOW_32_BITS, p, q);
    return FastMath.abs(a + b);
...
intgcdPositive(int a, int b)
Computes the greatest common divisor of two positive numbers (this precondition is not checked and the result is undefined if not fulfilled) using the "binary gcd" method which avoids division and modulo operations.
if (a == 0) {
    return b;
} else if (b == 0) {
    return a;
final int aTwos = Integer.numberOfTrailingZeros(a);
a >>= aTwos;
final int bTwos = Integer.numberOfTrailingZeros(b);
...