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

longGCD(long a, long b)
GCD
while (a != 0 && b != 0) {
    while ((b & 1) == 0) {
        b >>= 1;
    while ((a & 1) == 0) {
        a >>= 1;
    if (a > b) {
...
longgcd(long a, long b)
Greatest common divisor using Euclides algorithm.
while (b != 0) {
    long n = a % b;
    a = b;
    b = n;
return a;
longgcd(long u, long v)
Returns the greatest common divisor of a pair of numbers.
assert u > 0 && v > 0;
int u2s = ffs(u) - 1, v2s = ffs(v) - 1;
u >>= u2s;
v >>= v2s; 
while (u != v) {
    while (u < v) {
        v -= u;
        v >>= (ffs(v) - 1);
...
longgcd(long x, long y)
Implemented recursively with Euclidian Algorithm
return (y == 0) ? x : gcd(y, x % y);
longgcd(long x, long y)
gcd
if (x < 0)
    x = -x;
if (y < 0)
    y = -y;
if (x < y) {
    long temp = x;
    x = y;
    y = temp;
...
longgcd(long[] array)
Greatest Common Divisor according to Euclid's algorithm
int listSize = array.length;
long a, b, gcd1;
long ONE = (long) 1;
long[] arrayTmp = array.clone();
Arrays.sort(arrayTmp);
if (listSize == 0 || arrayTmp[listSize - 1] == 0) {
    return (ONE);
a = arrayTmp[0];
gcd1 = a;
for (int i = 1; i < listSize; i++) {
    if (gcd1 == 1) {
        break;
    gcd1 = a;
    a = arrayTmp[i];
    b = gcd1;
    while (b != 0) {
        gcd1 = b;
        b = a % gcd1;
        a = gcd1;
return (gcd1);
longgcd(long[] array)
Computes the greatest absolute common divisor of an integer array.
if (array.length == 0) {
    return 1;
long[] maxMinValue = maxMinValues(array);
long minAbsValue = Math.min(Math.abs(maxMinValue[0]), Math.abs(maxMinValue[1]));
for (long i = minAbsValue; i >= 1; i--) {
    int j;
    for (j = 0; j < array.length; ++j) {
...
longgcd1(long given1, long given2)
gcd
if (given1 == 0 || given2 == 0)
    return -1;
long a = given1 % given2;
long b = given2 % a;
long t = 0;
while (b != 0) {
    t = b;
    b = a % b;
...
longGCDHelper(long a, long b)
GCD Helper
if (b == 0) {
    return a;
return GCDHelper(b, a % b);
intgcdMultiple(int[] nums)
gcd Multiple
if (nums.length == 0)
    return 0;
Arrays.sort(nums);
int result = nums[0];
for (int i = 1; i < nums.length; ++i) {
    result = gcd(result, nums[i]);
return result;
...