Java gcd gcd(int a, int b)

Here you can find the source of gcd(int a, int b)

Description

Greatest common divisor.<br> http://en.wikipedia.org/wiki/Greatest_common_divisor<br> gcd( 6, 9 ) = 3<br> gcd( 4, 9 ) = 1<br> gcd( 0, 9 ) = 9 - see: http://math.stackexchange.com/questions/27719/what-is-gcd0-a-where-a-is-a-positive-integer<br> gcd( 0, 0 ) = 0 - this is the only situation when the result is zero.<br> gcd( 0, Integer.MIN_VALUE ) = Integer.MIN_VALUE<br> gcd( Integer.MIN_VALUE, 0 ) = Integer.MIN_VALUE<br> 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.<br> gcd( a, b ) = gcd( -a, b ) = gcd( a, -b ) = gcd( -a, -b ) = gcd( b, a )<br> The result is always positive except four exceptional situations described above.

License

Open Source License

Parameter

Parameter Description
a first number
b second number

Return

greatest common divisor of a and b

Declaration

public static int gcd(int a, int b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w w  w  . j ava2  s  .  c om
     * Greatest common divisor.<br>
     * http://en.wikipedia.org/wiki/Greatest_common_divisor<br>
     * gcd( 6, 9 ) = 3<br>
     * gcd( 4, 9 ) = 1<br>
     * gcd( 0, 9 ) = 9 - see: http://math.stackexchange.com/questions/27719/what-is-gcd0-a-where-a-is-a-positive-integer<br>
     * gcd( 0, 0 ) = 0 - this is the only situation when the result is zero.<br>
     * gcd( 0, Integer.MIN_VALUE ) = Integer.MIN_VALUE<br>
     * gcd( Integer.MIN_VALUE, 0 ) = Integer.MIN_VALUE<br>
     * 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.<br>
     * gcd( a, b ) = gcd( -a, b ) = gcd( a, -b ) = gcd( -a, -b ) = gcd( b, a )<br>
     * The result is always positive except four exceptional situations described above.
     * @param a first number
     * @param b second number
     * @return greatest common divisor of a and b
     */
    public static int gcd(int a, int b) {
        if (a == 0)
            return b < 0 ? -b : b;
        if (b == 0)
            return a < 0 ? -a : a;
        if (a < 0) {
            // Integer.MIN_VALUE is power of two, so greatest common divisor is lowest set bit of second argument.
            // See: Integer.lowestOneBit.
            if (a == Integer.MIN_VALUE)
                return b & -b;
            a = -a;
        }
        if (b < 0) {
            if (b == Integer.MIN_VALUE)
                return a & -a;
            b = -b;
        }
        // Euclidean algorithm.
        // Binary algorithm seems to be slower on modern computers.
        // Both algorithms have the same asymptotics.
        while (b > 0) {
            int c = a % b;
            a = b;
            b = c;
        }
        return a;
    }

    /**
     * Greatest common divisor.<br>
     * http://en.wikipedia.org/wiki/Greatest_common_divisor<br>
     * gcd( 6, 9 ) = 3<br>
     * gcd( 4, 9 ) = 1<br>
     * gcd( 0, 9 ) = 9 - see: http://math.stackexchange.com/questions/27719/what-is-gcd0-a-where-a-is-a-positive-integer<br>
     * gcd( 0, 0 ) = 0 - this is the only situation when the result is zero.<br>
     * gcd( 0, Long.MIN_VALUE ) = Long.MIN_VALUE<br>
     * gcd( Long.MIN_VALUE, 0 ) = Long.MIN_VALUE<br>
     * gcd( Long.MIN_VALUE, Long.MIN_VALUE ) = Long.MIN_VALUE
     * - these are the only situations when the result is negative,
     * because abs( Long.MIN_VALUE ) cannot fit in long.<br>
     * gcd( a, b ) = gcd( -a, b ) = gcd( a, -b ) = gcd( -a, -b ) = gcd( b, a )<br>
     * The result is always positive except four exceptional situations described above.
     * @param a first number
     * @param b second number
     * @return greatest common divisor of a and b
     */
    public static long gcd(long a, long b) {
        if (a == 0L)
            return b < 0L ? -b : b;
        if (b == 0L)
            return a < 0L ? -a : a;
        if (a < 0L) {
            // Long.MIN_VALUE is power of two, so greatest common divisor is lowest set bit of second argument.
            // See: Long.lowestOneBit.
            if (a == Long.MIN_VALUE)
                return b & -b;
            a = -a;
        }
        if (b < 0L) {
            if (b == Long.MIN_VALUE)
                return a & -a;
            b = -b;
        }
        if (a <= Integer.MAX_VALUE && b <= Integer.MAX_VALUE) {
            int aa = (int) a;
            int bb = (int) b;
            while (bb > 0) {
                int c = aa % bb;
                aa = bb;
                bb = c;
            }
            return aa;
        }
        while (b > 0L) {
            long c = a % b;
            a = b;
            b = c;
        }
        return a;
    }
}

Related

  1. gcd(int a, int b)
  2. gcd(int a, int b)
  3. gcd(int a, int b)
  4. gcd(int a, int b)
  5. gcd(int a, int b)
  6. gcd(int a, int b)
  7. gcd(int a, int b)
  8. gcd(int a, int b)
  9. gcd(int a, int b)