Java Utililty Methods gcd

List of utility methods to do gcd

Description

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

Method

intgcd(int u, int v)
Modern Euclidian algorithm
int t = 0;
if (u < v) {
    t = u;
    u = v;
    v = t;
while (v != 0) {
    t = u % v;
...
intgcd(int x, int y)
Calculate two number's greatest common divisor.
int temp;
while (y != 0) {
    temp = x % y;
    x = y;
    y = temp;
return x;
intgcd(int x1, int x2)
Method that calculates the Greatest Common Divisor (GCD) of two positive integer numbers.
if (x1 < 0 || x2 < 0) {
    throw new IllegalArgumentException("Cannot compute the GCD " + "if one integer is negative.");
int a, b, g, z;
if (x1 > x2) {
    a = x1;
    b = x2;
} else {
...
intgcd(int[] array)
Computes the greatest absolute common divisor of an integer array.
if (array.length == 0) {
    return 1;
int[] maxMinValue = maxMinValues(array);
int minAbsValue = Math.min(Math.abs(maxMinValue[0]), Math.abs(maxMinValue[1]));
for (int i = minAbsValue; i >= 1; i--) {
    int j;
    for (j = 0; j < array.length; ++j) {
...
intgcd(Integer... values)
Calculates the greatest common divisor of the specified integer numbers.
if (values.length == 0)
    return 1;
int allSgn = signum(values[0].intValue());
int gcd = values[0].intValue();
for (int i = 1; i < values.length; i++) {
    if (allSgn != signum(values[i].intValue()))
        allSgn = 1;
    if (gcd == 0 || gcd == 1)
...
longgcd(long a, long b)
gcd
long r = b;
while (b > 0) {
    r = b;
    b = a % b;
    a = r;
return r;
longgcd(long a, long b)
gcd
long r = a;
a = Math.max(a, b);
b = Math.min(r, b);
r = b;
while (a % b != 0) {
    r = a % b;
    a = b;
    b = r;
...
longGCD(long a, long b)
GCD
if (b == 0)
    return a;
return GCD(b, a % b);
longgcd(long a, long b)
Return the greatest common divisor of a and b, consistently with BigInteger#gcd(BigInteger) .
a = Math.abs(a);
b = Math.abs(b);
if (a == 0) {
    return b;
} else if (b == 0) {
    return a;
final int commonTrailingZeros = Long.numberOfTrailingZeros(a | b);
...
longgcd(long a, long b)
Calculates greatest common divisor (GCD) of two integer values a and b
if (a == 0)
    return b;
if (b == 0)
    return a;
if (a == b)
    return a;
if (a == 1 | b == 1)
    return 1;
...