代码拉取完成,页面将自动刷新
/**
* An implementation of finding the LCM of two numbers
*
* <p>Time Complexity ~O(log(a + b))
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.math;
public class LCM {
// Finds the greatest common divisor of a and b
private static long gcd(long a, long b) {
return b == 0 ? (a < 0 ? -a : a) : gcd(b, a % b);
}
// Finds the least common multiple of a and b
public static long lcm(long a, long b) {
long lcm = (a / gcd(a, b)) * b;
return lcm > 0 ? lcm : -lcm;
}
public static void main(String[] args) {
System.out.println(lcm(12, 18)); // 36
System.out.println(lcm(-12, 18)); // 36
System.out.println(lcm(12, -18)); // 36
System.out.println(lcm(-12, -18)); // 36
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。