Java lcm lcm(int a, int b)

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

Description

Computes the least common multiple of two integers.

License

EUPL

Parameter

Parameter Description
a a parameter
b a parameter

Declaration

public static int lcm(int a, int b) 

Method Source Code

//package com.java2s;
/*/*from  w w  w .  j  ava2  s.  c  o  m*/
* Copyright 2013 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or ? as soon they will be approved 
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and 
* limitations under the Licence.
*/

public class Main {
    /**
     * Computes the least common multiple of two integers.
     *
     * @param a
     * @param b
     * @return
     */
    public static int lcm(int a, int b) {
        return a * (b / gcd(a, b));
    }

    /**
     * Computes the greatest common divisor of two integers.
     *
     * @param a
     * @param b
     * @return
     */
    public static int gcd(int a, int b) {
        while (b > 0) {
            int temp = b;
            b = a % b; // % is remainder  
            a = temp;
        }
        return a;
    }
}

Related

  1. lcm(int a, int 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)
  8. LCM(int m, int n)
  9. lcm(int m, int n)