Java lcm lcm(long a, long b)

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

Description

Calculates lowest common multiple (LCM) of two integer values a and b

License

LGPL

Parameter

Parameter Description
a Value a
b Value b

Declaration

public static long lcm(long a, long b) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**//  w ww .ja v a 2  s  . com
     * Calculates lowest common multiple (LCM) of two integer values
     * <code>a</code> and <code>b</code>
     * 
     * @param a
     *            Value a
     * @param b
     *            Value b
     */
    public static long lcm(long a, long b) {
        return (a * b) / gcd(a, b);
    }

    /**
     * Calculates greatest common divisor (GCD) of two integer values
     * <code>a</code> and <code>b</code>
     * 
     * @param a
     *            Value a
     * @param b
     *            Value b
     */
    public static long gcd(long a, long b) {
        if (a == 0)
            return b;
        if (b == 0)
            return a;
        if (a == b)
            return a;
        if (a == 1 | b == 1)
            return 1;
        if ((a % 2 == 0) & (b % 2 == 0))
            return 2 * gcd(a / 2, b / 2);
        if ((a % 2 == 0) & (b % 2 != 0))
            return gcd(a / 2, b);
        if ((a % 2 != 0) & (b % 2 == 0))
            return gcd(a, b / 2);
        return gcd(b, Math.abs(a - b));
    }
}

Related

  1. lcm(int num1, int num2)
  2. lcm(int numberOne, int numberTwo)
  3. lcm(int p, int q)
  4. lcm(int x1, int x2)
  5. lcm(long a, long b)
  6. lcm(long a, long b)
  7. lcm(long u, long v)
  8. lcm(long x, long y)
  9. lcm(long[] a)