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