## The challenge

You will have a list of rationals in the form

 ``````1 2 3 4 5 `````` ``````{ {numer_1, denom_1} , ... {numer_n, denom_n} } or [ [numer_1, denom_1] , ... [numer_n, denom_n] ] or [ (numer_1, denom_1) , ... (numer_n, denom_n) ] ``````

where all numbers are positive ints. You have to produce a result in the form:

 ``````1 2 3 4 5 6 7 8 9 `````` ``````(N_1, D) ... (N_n, D) or [ [N_1, D] ... [N_n, D] ] or [ (N_1', D) , ... (N_n, D) ] or {{N_1, D} ... {N_n, D}} or "(N_1, D) ... (N_n, D)" ``````

depending on the language (See Example tests)

in which D is as small as possible and

 ``````1 `````` ``````N_1/D == numer_1/denom_1 ... N_n/D == numer_n,/denom_n. ``````

Example:

 ``````1 `````` ``````convertFracs [(1, 2), (1, 3), (1, 4)] `shouldBe` [(6, 12), (4, 12), (3, 12)] ``````

#### Note:

Due to the fact that the first translations were written long ago – more than 6 years – these first translations have only irreducible fractions.

Newer translations have some reducible fractions. To be on the safe side it is better to do a bit more work by simplifying fractions even if they don’t have to be.

#### Note for Bash:

input is a string, e.g `"2,4,2,6,2,8"` output is then `"6 12 4 12 3 12"`

## The solution in Java code

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 `````` ``````import static java.math.BigInteger.valueOf; import static java.util.Arrays.stream; import static java.util.stream.Collectors.joining; import java.util.function.LongBinaryOperator; class Fracts { static LongBinaryOperator gcd = (a, b) -> valueOf(a).gcd(valueOf(b)).longValue(); static LongBinaryOperator lcm = (a, b) -> b / gcd.applyAsLong(a, b) * a; static String convertFrac(long[][] lst) { if (lst.length == 0) return ""; long lcm = stream(lst).map(r -> r[1]).reduce(lst[0][1], Fracts.lcm::applyAsLong); long gcd = stream(lst).map(r -> lcm * r[0] / r[1]).reduce(lcm, Fracts.gcd::applyAsLong); var str = stream(lst).mapToLong(r -> lcm * r[0] / r[1] / gcd).mapToObj(Long::toString); return "(" + str.collect(joining("," + lcm / gcd + ")(")) + "," + lcm / gcd + ")"; } } ``````

Option 2:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 `````` ``````public class Fracts { public static String convertFrac(long[][] list) { reduceFractions(list); long lcm = calculateTotalLcm(list); StringBuilder result = new StringBuilder(); for(long[] fraction : list) { long ratio = lcm / fraction[1]; result.append('(') .append(fraction[0] * ratio).append(',').append(fraction[1] * ratio) .append(')'); } return result.toString(); } private static void reduceFractions(long[][] list) { for(int i = 0; i < list.length; i++) { long[] fraction = list[i]; long gcd = gcd(fraction[0], fraction[1]); fraction[0] = fraction[0] / gcd; fraction[1] = fraction[1] / gcd; } } private static long calculateTotalLcm(long[][] list) { long lcm = 1L; for(long[] fraction : list) { lcm = lcm(lcm, fraction[1]); } return lcm; } private static long lcm(long a, long b) { return a * b / gcd(a,b); } private static long gcd(long a, long b) { if(b == 0) { return a; } else { return gcd(b, a % b); } } } ``````

Option 3:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 `````` ``````import java.util.Arrays; public class Fracts { public static String convertFrac(long[][] lst) { StringBuilder result = new StringBuilder(); long[] d = new long[lst.length]; for(int i = 0; i

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; public class FractsTest { @Test public void test_fractions() throws Exception { long[][] lst; lst = new long[][] { {1, 2}, {1, 3}, {10, 40} }; assertEquals("(6,12)(4,12)(3,12)", Fracts.convertFrac(lst)); } } ``````

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 `````` ``````import static org.junit.Assert.*; import org.junit.Test; public class FractsTest { private static String Array2D2String(long[][] lst) { String s = "["; int l = lst.length; for (int i = 0; i < l; i++) { long[] a = lst[i]; s += "[" + a[0] + ", " + a[1]; if (i < l-1) s += "], "; else s += "]"; } return s += "]"; } private static void testing(long[][] lst, String expected) { System.out.println("Testing " + Array2D2String(lst)); String actual = Fracts.convertFrac(lst); System.out.println("Actual " + actual); System.out.println("Expect " + expected); System.out.println("-"); assertEquals(expected, actual); } @Test public void test() { long[][] lst = new long[][] { {1, 2}, {1, 3}, {1, 4} }; testing(lst, "(6,12)(4,12)(3,12)"); lst = new long[][] { {69, 130}, {87, 1310}, {30, 40} }; testing(lst, "(18078,34060)(2262,34060)(25545,34060)"); lst = new long[][] { }; testing(lst, ""); lst = new long[][] { {77, 130}, {84, 131}, {3, 4} }; testing(lst, "(20174,34060)(21840,34060)(25545,34060)"); lst = new long[][] { {6, 13}, {187, 1310}, {31, 41} }; testing(lst, "(322260,698230)(99671,698230)(527930,698230)"); lst = new long[][] { {8, 15}, {7, 111}, {4, 25} }; testing(lst, "(1480,2775)(175,2775)(444,2775)"); lst = new long[][] { {1, 2}, {1, 3}, {1, 4} }; testing(lst, "(6,12)(4,12)(3,12)"); lst = new long[][] { {77, 130}, {840, 1310}, {3, 4} }; testing(lst, "(20174,34060)(21840,34060)(25545,34060)"); lst = new long[][] { {1, 100}, {30, 10000}, {1, 2500}, {1, 20000} }; testing(lst, "(200,20000)(60,20000)(8,20000)(1,20000)"); lst = new long[][] { {1, 1}, {3, 1}, {4, 1}, {5, 1} }; testing(lst, "(1,1)(3,1)(4,1)(5,1)"); lst = new long[][] { {3, 1} }; testing(lst, "(3,1)"); lst = new long[][] { {77, 130}, {84, 131}, {30, 40} }; testing(lst, "(20174,34060)(21840,34060)(25545,34060)"); lst = new long[][] { {1, 100}, {3, 1000}, {1, 2500}, {1, 20000} }; testing(lst, "(200,20000)(60,20000)(8,20000)(1,20000)"); } private static int randInt(int min, int max) { return (int)(min + Math.random() * ((max - min) + 1)); } // private static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } private static long lcm(long a, long b) { return a * b / gcd(a, b); } private static String convertFracHD(long[][] lst) { long lcmall = 1; long[][] newlst = new long[lst.length][2]; for (int i = 0; i < lst.length; i++) { long g = gcd(lst[i][0], lst[i][1]); newlst[i][0] = lst[i][0] / g; newlst[i][1] = lst[i][1] / g; } //System.out.println(Arrays.deepToString(newlst)); for (long[] item : newlst) { lcmall = lcm(lcmall, item[1]); } String result = ""; for (long[] item : newlst) { result += "(" + (item[0] * lcmall / item[1]) + "," + lcmall + ")"; } return result; } // private static long[][] doEx(int n) { long[][] out = new long[n][2]; int j = 0; while (j < n) { out[j][0] = randInt(10, 80); out[j][1] = randInt(2, 5) * out[j][0]; j += 1; } return out; } @Test public void test1() { System.out.println("Random Tests ****"); for (int i = 0; i < 100; i++) { long[][] v = doEx(randInt(5, 10)); String exp = convertFracHD(v); testing(v, exp); } } } ``````