Java lcm lcm(int num1, int num2)

Here you can find the source of lcm(int num1, int num2)

Description

lcm

License

Open Source License

Declaration

public static int lcm(int num1, int num2) 

Method Source Code

//package com.java2s;
/**//from   ww  w  .  j a v  a  2  s . c om
 * <copyright> 
 *
 * Copyright (c) 2008 Fabiano Cruz (UFAM - Universidade Federal do Amazonas) 
 * and others. All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Fabiano Cruz [fabianoc at acm.org] - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: Util.java,v 1.3 2008/01/20 15:48:44 fabianocruz Exp $
 */

public class Main {
    public static int lcm(int num1, int num2) {
        if (num1 != 0 && num2 != 0)
            return (num2 / gdc(num1, num2)) * num1;
        return 0;
    }

    /**
     * the Euclidean algorithm for the GDC (greatest common divisor) also gives
     * us a fast algorithm for the LCM (least common multiple or lowest common
     * multiple).
     * 
     * example: lcm(21,6) = 21*6 / gdc(21,6) = 21*6 / 3 = 126 / 3 = 42
     * 
     */
    public static int gdc(int num1, int num2) {
        if (num2 == 0)
            return num1;
        else
            return gdc(num2, num1 % num2);
    }
}

Related

  1. lcm(int a, int b)
  2. lcm(int m, int n)
  3. LCM(int m, int n)
  4. lcm(int n, int m)
  5. lcm(int num1, int num2)
  6. lcm(int numberOne, int numberTwo)
  7. lcm(int p, int q)
  8. lcm(int x1, int x2)
  9. lcm(long a, long b)