Here you can find the source of lcm(int num1, int num2)
public static int lcm(int num1, int num2)
//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); } }