List of utility methods to do gcd
int | gcd(int a, int b) This method finds the greatest common divisor: The largest integer or the polynomial (monomial) of highest degree that is an exact divisor or each of two or more integers or polynomials. int t; while (b != 0) { t = b; b = a % b; a = t; return a; |
int | gcd(int a, int b) Greatest common divisor. http://en.wikipedia.org/wiki/Greatest_common_divisor gcd( 6, 9 ) = 3 gcd( 4, 9 ) = 1 gcd( 0, 9 ) = 9 - see: http://math.stackexchange.com/questions/27719/what-is-gcd0-a-where-a-is-a-positive-integer gcd( 0, 0 ) = 0 - this is the only situation when the result is zero. gcd( 0, Integer.MIN_VALUE ) = Integer.MIN_VALUE gcd( Integer.MIN_VALUE, 0 ) = Integer.MIN_VALUE gcd( Integer.MIN_VALUE, Integer.MIN_VALUE ) = Integer.MIN_VALUE - these are the only situations when the result is negative, because abs( Integer.MIN_VALUE ) cannot fit in int. gcd( a, b ) = gcd( -a, b ) = gcd( a, -b ) = gcd( -a, -b ) = gcd( b, a ) The result is always positive except four exceptional situations described above. if (a == 0) return b < 0 ? -b : b; if (b == 0) return a < 0 ? -a : a; if (a < 0) { if (a == Integer.MIN_VALUE) return b & -b; a = -a; ... |
int | gcd(int a, int b) gcd if (b != 0) return gcd(b, a % b); else return a; |
int | gcd(int a, int b) Find the greatest common divisor of two integers. if (b == 0) { return a; } else { return gcd(b, a % b); |
int | gcd(int a, int b) gcd while (b > 0) { int tmp = b; b = a % b; a = tmp; return a; |
int | gcd(int a, int b) gcd if (a < 0) a = -a; if (b < 0) b = -b; int temp; while (b != 0) { temp = b; b = a % b; ... |
int | gcd(int a, int b, int... rest) gcd if (rest.length > 0) { int[] rest1 = new int[rest.length - 1]; System.arraycopy(rest, 1, rest1, 0, rest1.length); return gcd(gcd(a, b), rest[0], rest1); return b == 0 ? a : gcd(b, a % b); |
int | gcd(int firstNumber, int secondNumber) gcd while (firstNumber % secondNumber != 0) { int temp = secondNumber; secondNumber = firstNumber % secondNumber; firstNumber = temp; return secondNumber; |
int | gcd(int k, int m) gcd int larger = k; int smaller = m; if (m > k) { smaller = k; larger = m; while (true) { larger -= smaller; ... |
int | gcd(int largerNumber, int smallerNumber) gcd return (smallerNumber == 0) ? largerNumber : gcd(smallerNumber, largerNumber % smallerNumber);
|