Java lcm lcm(int a, int b)

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

Description

Least common multiple.<br> http://en.wikipedia.org/wiki/Least_common_multiple<br> lcm( 6, 9 ) = 18<br> lcm( 4, 9 ) = 36<br> lcm( 0, 9 ) = 0<br> lcm( 0, 0 ) = 0

License

Open Source License

Parameter

Parameter Description
a first number
b second number

Return

least common multiple of a and b

Declaration

public static int lcm(int a, int b) 

Method Source Code

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

public class Main {
    /**/* w  w  w.j a  va2 s.co m*/
     * Least common multiple.<br>
     * http://en.wikipedia.org/wiki/Least_common_multiple<br>
     * lcm( 6, 9 ) = 18<br>
     * lcm( 4, 9 ) = 36<br>
     * lcm( 0, 9 ) = 0<br>
     * lcm( 0, 0 ) = 0
     * @param a first number
     * @param b second number
     * @return least common multiple of a and b
     */
    public static int lcm(int a, int b) {
        if (a == 0 || b == 0)
            return 0;
        return Math.abs(a / gcd(a, b) * b);
    }

    /**
     * Least common multiple.<br>
     * http://en.wikipedia.org/wiki/Least_common_multiple<br>
     * lcm( 6, 9 ) = 18<br>
     * lcm( 4, 9 ) = 36<br>
     * lcm( 0, 9 ) = 0<br>
     * lcm( 0, 0 ) = 0
     * @param a first number
     * @param b second number
     * @return least common multiple of a and b
     */
    public static long lcm(long a, long b) {
        if (a == 0L || b == 0L)
            return 0L;
        return Math.abs(a / gcd(a, b) * b);
    }

    /**
     * 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. LCM(double a, double b)
  2. lcm(int a, int b)
  3. lcm(int a, int b)
  4. lcm(int a, int b)
  5. lcm(int a, int b)
  6. lcm(int a, int b)
  7. lcm(int a, int b)