src/org/kocakosm/pitaya/math/Numbers.java
public static long gcd(long a, long b)
	{
		if (a < 0L || b < 0L) {
			try {
				return gcd(safeAbs(a), safeAbs(b));
			} catch (ArithmeticException e) {
				throw new ArithmeticException(
					"Long overflow: gcd(" + a + ", " + b + ")");
			}
		}
		if (a == 0L) {
			return b;
		}
		if (b == 0L) {
			return a;
		}
		int shift = 0;
		while (((a | b) & 1L) == 0) {
			a >>= 1;
			b >>= 1;
			shift++;
		}
		while ((a & 1L) == 0L) {
			a >>= 1;
		}
		do {
			while ((b & 1L) == 0L) {
				b >>= 1;
			}
			if (a > b) {
				long tmp = b;
				b = a;
				a = tmp;
			}
			b -= a;
		} while (b != 0L);
		return a << shift;
	}
public static long gcd(long a, long b)
	{
		if (a < 0L || b < 0L) {
			try {
				return gcd(safeAbs(a), safeAbs(b));
			} catch (ArithmeticException e) {
				throw new ArithmeticException(
					"Long overflow: gcd(" + a + ", " + b + ")");
			}
		}
		if (a == 0L) {
			return b;
		}
		if (b == 0L) {
			return a;
		}
		int shift = 0;
		while (((a | b) & 1L) == 0) {
			a >>= 1;
			b >>= 1;
			shift++;
		}
		while ((a & 1L) == 0L) {
			a >>= 1;
		}
		do {
			while ((b & 1L) == 0L) {
				b >>= 1;
			}
			if (a > b) {
				long tmp = b;
				b = a;
				a = tmp;
			}
			b -= a;
		} while (b != 0L);
		return a << shift;
	}
public static long gcd ( long a , long b ) { if ( a < 0L || b < 0L ) { try { return gcd ( safeAbs ( a ) , safeAbs ( b ) ) ; } catch ( ArithmeticException e ) { throw new ArithmeticException ( "Long overflow: gcd(" + a + ", " + b + ")" ) ; } } if ( a == 0L ) { return b ; } if ( b == 0L ) { return a ; } int shift = 0 ; while ( ( ( a | b ) & 1L ) == 0 ) { a >>= 1 ; b >>= 1 ; shift ++ ; } while ( ( a & 1L ) == 0L ) { a >>= 1 ; } do { while ( ( b & 1L ) == 0L ) { b >>= 1 ; } if ( a > b ) { long tmp = b ; b = a ; a = tmp ; } b -= a ; } while ( b != 0L ) ; return a << shift ; }
Returns the greatest common divisor of the absolute value of the
given two numbers. This method first computes the absolute values of
the given numbers, thus it doesn't accept {@code Long.MIN_VALUE}.
Also, note that if one of the given values is {@code 0}, this method
will return the absolute value of the second argument.
This method implements the binary GCD algorithm (also known as
Stein's algorithm). For further information on this algorithm, please
refer to <a href="http://en.wikipedia.org/wiki/Binary_GCD_algorithm">
Wikipedia</a>.

@param a the first number.
@param b the second number.

@return the greatest common divisor of {@code a} and {@code b}.

@throws ArithmeticException if {@code a} or {@code b} is equal to
{@code Long.MIN_VALUE}.
Returns the greatest common divisor of the absolute value of the given two numbers . This method first computes the absolute values of the given numbers thus it doesn t accept { @code Long . MIN_VALUE } . Also note that if one of the given values is { @code 0 } this method will return the absolute value of the second argument . This method implements the binary GCD algorithm ( also known as Stein s algorithm ) . For further information on this algorithm please refer to <a href = http : // en . wikipedia . org / wiki / Binary_GCD_algorithm > Wikipedia< / a > .