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 provideTestCases() { - return Stream.of(Arguments.of(12, 18, 36), Arguments.of(5, 10, 10), Arguments.of(7, 3, 21), Arguments.of(21, 6, 42), Arguments.of(1, 1, 1), Arguments.of(8, 12, 24), Arguments.of(14, 35, 70), Arguments.of(15, 25, 75), Arguments.of(100, 25, 100), Arguments.of(0, 10, 0)); + return Stream.of(Arguments.of(12, 18, 36), Arguments.of(5, 10, 10), Arguments.of(7, 3, 21), Arguments.of(21, 6, 42), Arguments.of(1, 1, 1), Arguments.of(8, 12, 24), Arguments.of(14, 35, 70), Arguments.of(15, 25, 75), Arguments.of(100, 25, 100), Arguments.of(0, 10, 0), Arguments.of(10, 0, 0), + Arguments.of(0, 0, 0), Arguments.of(-12, 18, 36), Arguments.of(12, -18, 36), Arguments.of(-12, -18, 36), Arguments.of(60_000, 90_000, 180_000)); + } + + @Test + void testIntOverflowThrows() { + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(50_000, 50_001)); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(Integer.MIN_VALUE, 1)); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(1, Integer.MIN_VALUE)); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(Integer.MIN_VALUE, 2)); + } + + @Test + void testIntVarargs() { + assertEquals(24, LeastCommonMultiple.lcm(4, 6, 8, 12)); + assertEquals(42, LeastCommonMultiple.lcm(-6, 14, 21)); + assertEquals(7, LeastCommonMultiple.lcm(new int[] {-7})); + assertEquals(0, LeastCommonMultiple.lcm(1_000_000_000, 1_000_000_001, 0)); + Supplier nullIntArray = () -> null; + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(nullIntArray.get())); + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new int[] {})); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(new int[] {Integer.MIN_VALUE})); + } + + @Test + void testLongLcm() { + assertEquals(6_000_000_000_000L, LeastCommonMultiple.lcm(2_000_000_000_000L, -3_000_000_000_000L)); + assertEquals(0L, LeastCommonMultiple.lcm(0L, Long.MAX_VALUE)); + assertEquals(0L, LeastCommonMultiple.lcm(Long.MIN_VALUE, 0L)); + assertEquals(60L, LeastCommonMultiple.lcm(12L, -15L, 20L)); + assertEquals(9L, LeastCommonMultiple.lcm(new long[] {-9L})); + assertEquals(0L, LeastCommonMultiple.lcm(4_000_000_000L, 4_000_000_001L, 0L)); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(4_000_000_000L, 4_000_000_001L)); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(Long.MIN_VALUE, 1L)); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(1L, Long.MIN_VALUE)); + Supplier nullLongArray = () -> null; + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(nullLongArray.get())); + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new long[] {})); + assertThrows(ArithmeticException.class, () -> LeastCommonMultiple.lcm(new long[] {Long.MIN_VALUE})); + } + + @Test + void testBigIntegerLcm() { + BigInteger a = BigInteger.valueOf(Long.MAX_VALUE); + BigInteger b = BigInteger.valueOf(Long.MAX_VALUE - 1); + assertEquals(a.multiply(b), LeastCommonMultiple.lcm(a, b)); + assertEquals(BigInteger.ZERO, LeastCommonMultiple.lcm(BigInteger.ZERO, a)); + assertEquals(BigInteger.ZERO, LeastCommonMultiple.lcm(a, BigInteger.ZERO)); + assertEquals(BigInteger.ZERO, LeastCommonMultiple.lcm(BigInteger.ZERO, BigInteger.TEN, BigInteger.valueOf(15))); + assertEquals(BigInteger.valueOf(7), LeastCommonMultiple.lcm(new BigInteger[] {BigInteger.valueOf(-7)})); + assertEquals(BigInteger.valueOf(60), LeastCommonMultiple.lcm(BigInteger.valueOf(12), BigInteger.valueOf(-15), BigInteger.valueOf(20))); + Supplier nullBigArray = () -> null; + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm((BigInteger) null, BigInteger.ONE)); + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(BigInteger.ONE, (BigInteger) null)); + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(nullBigArray.get())); + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new BigInteger[] {BigInteger.TEN, null})); + assertThrows(IllegalArgumentException.class, () -> LeastCommonMultiple.lcm(new BigInteger[] {})); } }