@@ -0,0 +1,2434 @@
+package me.gowdru.notes.math;%0A%0Aimport java.io.*;%0Aimport java.util.*;%0A/**%0A * Created by tg on 3/20/16.%0A */%0Apublic class FractionReducer %7B%0A%0A    public static class Fraction %7B%0A        public int n, d;%0A        public Fraction(int n, int d)%7B%0A            this.n = n; this.d = d;%0A        %7D%0A        @Override%0A        public String toString()%7B%0A            return String.format(%22%25d/%25d%22, n, d);%0A        %7D%0A    %7D%0A%0A    public static int lcm(int a, int b)%7B%0A        int max = a, min = b;%0A        if (a %3C b)%7B%0A            min = a;%0A            max = b;%0A        %7D%0A        int lcm = max;%0A        while(lcm %25 min != 0)%7B%0A            lcm += max;%0A        %7D%0A        return lcm;%0A    %7D%0A%0A    public static int lcm(int...ns)%7B%0A        if(ns.length == 1)%7B%0A            return ns%5B0%5D;%0A        %7D%0A        int lcm = lcm(ns%5B0%5D, ns%5B1%5D);%0A        for(int i=2; i %3Cns.length; i++)%7B%0A            lcm = lcm(lcm, ns%5Bi%5D);%0A        %7D%0A        return lcm;%0A    %7D%0A%0A    public static int gcd(int a, int b)%7B%0A        while (b != 0) %7B%0A            int tmp = a %25 b;%0A            a = b;%0A            b = tmp;%0A        %7D%0A        return a;%0A    %7D%0A%0A    public static int gcd(int...ns)%7B%0A        int gcd = gcd(ns%5B0%5D, ns%5B1%5D);%0A        for(int i=2; i %3Cns.length; i++)%7B%0A            gcd = gcd(gcd, ns%5Bi%5D);%0A        %7D%0A        return gcd;%0A    %7D%0A%0A    public static Fraction sum(List%3CFraction%3E fractions)%7B%0A        //denominators = LCM of denominnators%0A        int ds%5B%5D = new int%5Bfractions.size()%5D;%0A        for (int i = 0; i %3C fractions.size(); i++) %7B%0A            ds%5Bi%5D = fractions.get(i).d;%0A        %7D%0A        int lcm = lcm(ds);%0A%0A        //numerator = (LCM/denominator)*numerator + .....%0A        int numr = 0;%0A        for (Fraction fraction : fractions) %7B%0A            numr += (lcm / fraction.d) * fraction.n;%0A        %7D%0A        int gcd = gcd(numr, lcm);%0A        return new Fraction(numr/gcd, lcm/gcd);%0A    %7D%0A%0A%0A    public static void main(String%5B%5D args) %7B%0A%0A        String src = %221%5Cn%22 +%0A                %223%5Cn%22 +%0A                %224 5%5Cn%22 +%0A                %222 1%5Cn%22 +%0A                %221 3%22;%0A        Scanner in = new Scanner(src);%0A        int t = in.nextInt();%0A        for (int i = 0; i %3C t; i++) %7B%0A            int n = in.nextInt();%0A            List%3CFraction%3E fractions = new ArrayList%3C%3E();%0A            for (int j = 0; j %3C n; j++)%7B%0A                fractions.add(new Fraction(in.nextInt(), in.nextInt()));%0A            %7D%0A            Fraction reduced = sum(fractions);%0A            System.out.println(reduced);%0A        %7D%0A    %7D%0A%7D%0A

Solve Fraction Reducer