-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFracts.java
33 lines (26 loc) · 1.12 KB
/
Fracts.java
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
import java.util.Arrays;
import java.util.stream.Collectors;
public class Fracts {
public static String convertFrac(long[][] lst) {
final Long dividor = Arrays.stream(lst)
.map(pair -> new Long[]{pair[0], pair[1]})
.map(Fracts::divideByGreatestCommonDivider)
.map(longs -> longs[1])
.reduce(1L, Fracts::leastCommonMultiple);
return Arrays.stream(lst)
.map(pair -> pair[0] * dividor / pair[1])
.map(aLong -> String.format("(%d,%d)", aLong, dividor))
.collect(Collectors.joining());
}
private static Long greatestCommonDivider(Long a, Long b) {
return b == 0 ? a : greatestCommonDivider(b, a % b);
}
private static Long[] divideByGreatestCommonDivider(Long[] pair) {
Long gcd = greatestCommonDivider(pair[0], pair[1]);
return new Long[]{pair[0] / gcd, pair[1] / gcd};
}
private static Long leastCommonMultiple(Long a, Long b) {
return Math.abs(a * b) / greatestCommonDivider(a, b);
}
}
//https://www.codewars.com/kata/54d7660d2daf68c619000d95/train/java