private static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); }
/** * Calculates the greatest common denominator (gcd) of an array of numbers * * @param numbers Array of numbers * @return gcd of array of numbers */ public static OptionalInt gcd(int[] numbers) { return Arrays.stream(numbers) .reduce((a, b) -> gcd(a, b)); }
/** * Calculates the lowest common multiple (lcm) of an array of numbers. * * @param numbers Array of numbers * @return lcm of array of numbers */ public static OptionalInt lcm(int[] numbers) { IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y); return Arrays.stream(numbers) .reduce((a, b) -> lcm.applyAsInt(a, b)); }
private static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); }
/** * Calculates the greatest common denominator (gcd) of an array of numbers * * @param numbers Array of numbers * @return gcd of array of numbers */ public static OptionalInt gcd(int[] numbers) { return Arrays.stream(numbers) .reduce((a, b) -> gcd(a, b)); }
/** * Calculates the lowest common multiple (lcm) of an array of numbers. * * @param numbers Array of numbers * @return lcm of array of numbers */ public static OptionalInt lcm(int[] numbers) { IntBinaryOperator lcm = (x, y) -> (x * y) / gcd(x, y); return Arrays.stream(numbers) .reduce((a, b) -> lcm.applyAsInt(a, b)); }