Repository navigation
Expand file tree
/
Copy pathLeastCommonMultiple.java
More file actions
181 lines (171 loc) · 6.39 KB
/
Copy pathLeastCommonMultiple.java
File metadata and controls
181 lines (171 loc) · 6.39 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.thealgorithms.maths;
import java.math.BigInteger;
/**
* Provides overflow-safe methods to compute the Least Common Multiple (LCM)
* of integers in {@code O(log(min(a, b)))} time via the Euclidean GCD
* algorithm.
*
* <p>Unlike {@code Math.abs(a * b) / gcd(a, b)}, these methods divide before
* multiplying and throw {@link ArithmeticException} if the exact result
* overflows {@code int} or {@code long}.
*
* @see <a href="https://en.wikipedia.org/wiki/Least_common_multiple">Least common multiple</a>
*/
public final class LeastCommonMultiple {
private LeastCommonMultiple() {
}
/**
* Finds the non-negative least common multiple of two {@code int} values.
*
* <p>Returns {@code 0} if either {@code num1} or {@code num2} is {@code 0}.
*
* @param num1 the first number
* @param num2 the second number
* @return the least common multiple of {@code num1} and {@code num2}
* @throws ArithmeticException if the exact result overflows {@code int}
*/
public static int lcm(int num1, int num2) {
if (num1 == 0 || num2 == 0) {
return 0;
}
long absA = Math.abs((long) num1);
long absB = Math.abs((long) num2);
long gcd = gcdLong(absA, absB);
// absA and absB are at most 2^31, so absA / gcd * absB <= 2^62 < Long.MAX_VALUE.
long result = absA / gcd * absB;
return Math.toIntExact(result);
}
/**
* Finds the non-negative least common multiple of one or more {@code int} values.
*
* <p>Returns {@code 0} if any element in {@code numbers} is {@code 0}.
*
* @param numbers non-empty array of integers
* @return the least common multiple of all elements
* @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
* @throws ArithmeticException if the exact result overflows {@code int}
*/
public static int lcm(int... numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Input array must not be null or empty.");
}
for (int number : numbers) {
if (number == 0) {
return 0;
}
}
int result = Math.toIntExact(Math.abs((long) numbers[0]));
for (int i = 1; i < numbers.length; i++) {
result = lcm(result, numbers[i]);
}
return result;
}
/**
* Finds the non-negative least common multiple of two {@code long} values.
*
* <p>Returns {@code 0L} if either {@code num1} or {@code num2} is {@code 0L}.
*
* @param num1 the first number
* @param num2 the second number
* @return the least common multiple of {@code num1} and {@code num2}
* @throws ArithmeticException if the exact result overflows {@code long}
*/
public static long lcm(long num1, long num2) {
if (num1 == 0L || num2 == 0L) {
return 0L;
}
if (num1 == Long.MIN_VALUE || num2 == Long.MIN_VALUE) {
throw new ArithmeticException("long overflow");
}
long absA = Math.abs(num1);
long absB = Math.abs(num2);
long gcd = gcdLong(absA, absB);
return Math.multiplyExact(absA / gcd, absB);
}
/**
* Finds the non-negative least common multiple of one or more {@code long} values.
*
* <p>Returns {@code 0L} if any element in {@code numbers} is {@code 0L}.
*
* @param numbers non-empty array of {@code long} values
* @return the least common multiple of all elements
* @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
* @throws ArithmeticException if the exact result overflows {@code long}
*/
public static long lcm(long... numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Input array must not be null or empty.");
}
for (long number : numbers) {
if (number == 0L) {
return 0L;
}
}
if (numbers[0] == Long.MIN_VALUE) {
throw new ArithmeticException("long overflow");
}
long result = Math.abs(numbers[0]);
for (int i = 1; i < numbers.length; i++) {
result = lcm(result, numbers[i]);
}
return result;
}
/**
* Finds the non-negative least common multiple of two {@link BigInteger} values.
*
* @param num1 the first number
* @param num2 the second number
* @return the least common multiple of {@code num1} and {@code num2}
* @throws IllegalArgumentException if either argument is {@code null}
*/
public static BigInteger lcm(BigInteger num1, BigInteger num2) {
if (num1 == null || num2 == null) {
throw new IllegalArgumentException("Numbers must not be null.");
}
if (num1.signum() == 0 || num2.signum() == 0) {
return BigInteger.ZERO;
}
BigInteger absA = num1.abs();
BigInteger absB = num2.abs();
return absA.divide(absA.gcd(absB)).multiply(absB);
}
/**
* Finds the non-negative least common multiple of one or more {@link BigInteger} values.
*
* @param numbers non-empty array of non-null {@link BigInteger} values
* @return the least common multiple of all elements
* @throws IllegalArgumentException if {@code numbers} is {@code null}, empty, or contains {@code null}
*/
public static BigInteger lcm(BigInteger... numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Input array must not be null or empty.");
}
boolean hasZero = false;
for (BigInteger number : numbers) {
if (number == null) {
throw new IllegalArgumentException("Elements must not be null.");
}
if (number.signum() == 0) {
hasZero = true;
}
}
if (hasZero) {
return BigInteger.ZERO;
}
BigInteger result = numbers[0].abs();
for (int i = 1; i < numbers.length; i++) {
result = lcm(result, numbers[i]);
}
return result;
}
private static long gcdLong(long a, long b) {
long curA = a;
long curB = b;
while (curB != 0L) {
long remainder = curA % curB;
curA = curB;
curB = remainder;
}
return curA;
}
}