Here you can find the source of lcm(int num1, int num2)
Parameter | Description |
---|---|
num1 | The first number |
num2 | The second number |
public static int lcm(int num1, int num2)
//package com.java2s; //License from project: LGPL public class Main { /**//from ww w . j av a 2s. com * Returns the least common multiple of two integers. * * @param num1 The first number * @param num2 The second number * @return The least common multiple */ public static int lcm(int num1, int num2) { if (num1 == 0 || num2 == 0) return 0; return num1 * num2 / gcd(num1, num2); } /** * Returns the greatest common divisor of two integers. * * @param num1 The first number * @param num2 The second number * @return The greatest common divisor */ public static int gcd(int num1, int num2) { if (num1 < 0) num1 = -num1; if (num2 < 0) num2 = -num2; return privGCD(num1, num2); } /** * Returns the greatest common divisor of two non-negative integers. * * @param num1 The first number; must be non-negative * @param num2 The second number; must be non-negative * @return The greatest common divisor */ private static int privGCD(int num1, int num2) { if (num1 == 0 && num2 == 0) return 1; if (num1 == 0) return num2; if (num2 == 0) return num1; return num1 > num2 ? privGCD(num1 % num2, num2) : privGCD(num1, num2 % num1); } }