forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcm.java
More file actions
30 lines (26 loc) · 802 Bytes
/
Lcm.java
File metadata and controls
30 lines (26 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Computes the Least Common Multiple (LCM) of two numbers using the relation LCM(a, b) = |a /
* gcd(a, b) * b|.
*
* <p>Time: ~O(log(a + b))
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.math;
public class Lcm {
/** Returns the least common multiple of a and b. The result is always non-negative. */
public static long lcm(long a, long b) {
return Math.abs(a / gcd(a, b) * b);
}
private static long gcd(long a, long b) {
if (b == 0)
return Math.abs(a);
return gcd(b, a % b);
}
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
}
}