/** Returns the greatest common divisor of the given absolute values. This implementation uses {@link #gcdPositive(int, int)} * and has the same special cases. * * @param args non-negative numbers * @return the greatest common divisor. */ public static int gcdPositive (int... args) { if (args == null || args.length < 2) throw new IllegalArgumentException("gcdPositive requires at least two arguments"); int result = args[0]; int n = args.length; for (int i = 1; i < n; i++) { result = gcdPositive(result, args[i]); } return result; }
/** Returns the least common multiple of the absolute value of two numbers, using the formula * {@code lcm(a, b) = (a / gcd(a, b)) * b}. * <p> * Special cases: * <ul> * <li>The invocations {@code lcm(Integer.MIN_VALUE, n)} and {@code lcm(n, Integer.MIN_VALUE)}, where {@code abs(n)} is a power * of 2, throw an {@code ArithmeticException}, because the result would be 2^31, which is too large for an {@code int} value.</li> * <li>The result of {@code lcm(0, x)} and {@code lcm(x, 0)} is {@code 0} for any {@code x}. * </ul> * * @param a a non-negative number. * @param b a non-negative number. * @return the least common multiple, never negative. * @throws ArithmeticException if the result cannot be represented as a non-negative {@code int} value. */ public static int lcmPositive (int a, int b) throws ArithmeticException { if (a == 0 || b == 0) return 0; int lcm = Math.abs(mulAndCheck(a / gcdPositive(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new ArithmeticException("overflow: lcm(" + a + ", " + b + ") > 2^31"); } return lcm; }
/** Returns the greatest common divisor of the given absolute values. This implementation uses {@link #gcdPositive(int, int)} * and has the same special cases. * * @param args non-negative numbers * @return the greatest common divisor. */ public static int gcdPositive (int... args) { if (args == null || args.length < 2) throw new IllegalArgumentException("gcdPositive requires at least two arguments"); int result = args[0]; int n = args.length; for (int i = 1; i < n; i++) { result = gcdPositive(result, args[i]); } return result; }
/** Returns the greatest common divisor of the given absolute values. This implementation uses {@link #gcdPositive(int, int)} * and has the same special cases. * * @param args non-negative numbers * @return the greatest common divisor. */ public static int gcdPositive (int... args) { if (args == null || args.length < 2) throw new IllegalArgumentException("gcdPositive requires at least two arguments"); int result = args[0]; int n = args.length; for (int i = 1; i < n; i++) { result = gcdPositive(result, args[i]); } return result; }
/** Returns the least common multiple of the absolute value of two numbers, using the formula * {@code lcm(a, b) = (a / gcd(a, b)) * b}. * <p> * Special cases: * <ul> * <li>The invocations {@code lcm(Integer.MIN_VALUE, n)} and {@code lcm(n, Integer.MIN_VALUE)}, where {@code abs(n)} is a power * of 2, throw an {@code ArithmeticException}, because the result would be 2^31, which is too large for an {@code int} value.</li> * <li>The result of {@code lcm(0, x)} and {@code lcm(x, 0)} is {@code 0} for any {@code x}. * </ul> * * @param a a non-negative number. * @param b a non-negative number. * @return the least common multiple, never negative. * @throws ArithmeticException if the result cannot be represented as a non-negative {@code int} value. */ public static int lcmPositive (int a, int b) throws ArithmeticException { if (a == 0 || b == 0) return 0; int lcm = Math.abs(mulAndCheck(a / gcdPositive(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new ArithmeticException("overflow: lcm(" + a + ", " + b + ") > 2^31"); } return lcm; }
/** Returns the least common multiple of the absolute value of two numbers, using the formula * {@code lcm(a, b) = (a / gcd(a, b)) * b}. * <p> * Special cases: * <ul> * <li>The invocations {@code lcm(Integer.MIN_VALUE, n)} and {@code lcm(n, Integer.MIN_VALUE)}, where {@code abs(n)} is a power * of 2, throw an {@code ArithmeticException}, because the result would be 2^31, which is too large for an {@code int} value.</li> * <li>The result of {@code lcm(0, x)} and {@code lcm(x, 0)} is {@code 0} for any {@code x}. * </ul> * * @param a a non-negative number. * @param b a non-negative number. * @return the least common multiple, never negative. * @throws ArithmeticException if the result cannot be represented as a non-negative {@code int} value. */ public static int lcmPositive (int a, int b) throws ArithmeticException { if (a == 0 || b == 0) return 0; int lcm = Math.abs(mulAndCheck(a / gcdPositive(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new ArithmeticException("overflow: lcm(" + a + ", " + b + ") > 2^31"); } return lcm; }