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 m, int n)
calculates the greatest common divisor of two numbers
if (m % n == 0)
    return n;
return gcd(n, m % n);
intGCD(int m, int n)
GCD
if (m < 0) {
    m = -m;
if (n < 0) {
    n = -n;
if (0 == n) {
    return m;
...
intgcd(int m, int n)
Returns the greatest common divisor (GCD) of two integer numbers.
if (m == 0 && n == 0)
    throw new IllegalArgumentException("Illegal call with " + "m = 0 and n = 0.");
m = Math.abs(m);
n = Math.abs(n);
while (m != 0) {
    int temporary = m;
    m = n % m;
    n = temporary;
...
intgcd(int n, int m)
Find the greatest common divisor of both n and m.
n = Math.abs(n);
m = Math.abs(m);
if (n == 0 && m == 0) {
    return 1;
if (n == m && n >= 1) {
    return n;
return (m < n) ? gcd(n - m, n) : gcd(n, m - n);
intgcd(int n1, int n2)
gcd
int dividend = 0;
int divisor = n1 > n2 ? n1 : n2;
int remainder = n1 > n2 ? n2 : n1;
while (remainder > 0) {
    dividend = divisor;
    divisor = remainder;
    remainder = dividend % divisor;
return divisor;
intgcd(int num1, int num2)
Returns the greatest common divisor of two integers.
if (num1 < 0)
    num1 = -num1;
if (num2 < 0)
    num2 = -num2;
return privGCD(num1, num2);
intgcd(int p, int q)
Calculates the greatest common divisor by using the Euclidean algorithm.
while (q != 0) {
    int r = q;
    q = p % q;
    p = r;
return p;
intgcd(int p, int q)
Computes the Greatest Common Devisor of integers p and q.
if (q == 0) {
    return p;
return gcd(q, p % q);
intgcd(int u, int v)
gcd
if (u == 0 || v == 0)
    return u | v;
int shift;
for (shift = 0; ((u | v) & 1) == 0; ++shift) {
    u >>= 1;
    v >>= 1;
while ((u & 1) == 0)
...
intgcd(int u, int v)

Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations.

if (u * v == 0) {
    return (Math.abs(u) + Math.abs(v));
if (u > 0) {
    u = -u;
if (v > 0) {
    v = -v;
...