diff --git a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java index 9ec11e9b3220..50fb582e3993 100644 --- a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java +++ b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java @@ -1,42 +1,181 @@ package com.thealgorithms.maths; +import java.math.BigInteger; + /** - * Is a common mathematics concept to find the smallest value number - * that can be divide using either number without having the remainder. - * https://maticschool.blogspot.com/2013/11/find-least-common-multiple-lcm.html - * @author LauKinHoong + * 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. + * + *
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 Least common multiple */ public final class LeastCommonMultiple { + private LeastCommonMultiple() { } /** - * Finds the least common multiple of two numbers. + * Finds the non-negative least common multiple of two {@code int} values. * - * @param num1 The first number. - * @param num2 The second number. - * @return The least common multiple of num1 and num2. + *
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) { - int high; - int num3; - int cmv = 0; - - if (num1 > num2) { - high = num1; - num3 = num1; - } else { - high = num2; - num3 = num2; - } - - while (num1 != 0) { - if (high % num1 == 0 && high % num2 == 0) { - cmv = high; - break; + 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. + * + *
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. + * + *
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. + * + *
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.");
}
- high += num3;
+ 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 cmv;
+ return curA;
}
}
diff --git a/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java b/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java
index 73da509723c5..fac3ff01aee9 100644
--- a/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java
+++ b/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java
@@ -1,8 +1,12 @@
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import java.math.BigInteger;
+import java.util.function.Supplier;
import java.util.stream.Stream;
+import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@@ -16,6 +20,62 @@ void testLcm(int num1, int num2, int expected) {
}
private static Stream