Widespread Denominators in Java | Discover ways to Grasp Software program Engineering, DevOps and Cloud


The problem

You’ll have a listing of rationals within the kind

{ {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) ] 

the place all numbers are optimistic ints. It’s a must to produce a consequence within the kind:

(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)"

relying on the language (See Instance checks)

during which D is as small as attainable and

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

Instance:

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

Be aware:

As a consequence of the truth that the primary translations had been written way back – greater than 6 years – these first translations have solely irreducible fractions.

Newer translations have some reducible fractions. To be on the secure facet it’s higher to do a bit extra work by simplifying fractions even when they don’t must be.

Be aware for Bash:

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

The answer in Java code

Possibility 1:

import static java.math.BigInteger.valueOf;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.becoming a member of;

import java.util.perform.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(lengthy[][] lst) {
    if (lst.size == 0) return "";

    lengthy lcm = stream(lst).map(r -> r[1]).scale back(lst[0][1], Fracts.lcm::applyAsLong);
    lengthy gcd = stream(lst).map(r -> lcm * r[0] / r[1]).scale back(lcm, Fracts.gcd::applyAsLong);
    var str = stream(lst).mapToLong(r -> lcm * r[0] / r[1] / gcd).mapToObj(Lengthy::toString);
    return "(" + str.acquire(becoming a member of("," + lcm / gcd + ")(")) + "," + lcm / gcd + ")";
  }
}

Possibility 2:

public class Fracts {
  public static String convertFrac(lengthy[][] checklist) {
    reduceFractions(checklist);
    lengthy lcm = calculateTotalLcm(checklist);
    StringBuilder consequence = new StringBuilder();
    for(lengthy[] fraction : checklist) {
      lengthy ratio = lcm / fraction[1];
      consequence.append('(')
            .append(fraction[0] * ratio).append(',').append(fraction[1] * ratio)
            .append(')');
    }
    return consequence.toString();
  }
  
  personal static void reduceFractions(lengthy[][] checklist) {
    for(int i = 0; i < checklist.size; i++) {
      lengthy[] fraction = checklist[i];
      lengthy gcd = gcd(fraction[0], fraction[1]);
      fraction[0] = fraction[0] / gcd;
      fraction[1] = fraction[1] / gcd;
    }
  }
  
  personal static lengthy calculateTotalLcm(lengthy[][] checklist) {
    lengthy lcm = 1L;
    for(lengthy[] fraction : checklist) {
      lcm = lcm(lcm, fraction[1]);
    }
    return lcm;
  }
  
  personal static lengthy lcm(lengthy a, lengthy b) {
    return a * b / gcd(a,b);
  }
  
  personal static lengthy gcd(lengthy a, lengthy b) {
    if(b == 0) {
      return a;
    } else {
      return gcd(b, a % b);
    }
  }
}

Possibility 3:

import java.util.Arrays;
public class Fracts {

 public static String convertFrac(lengthy[][] lst) {
        StringBuilder consequence = new StringBuilder();
        lengthy[] d = new lengthy[lst.length];
        for(int i = 0; i<lst.size;i++){
            lengthy[] r = lst[i];
            lengthy gcd = gcd(r[0],r[1]);
            r[0]/=gcd; r[1]/=gcd;
            d[i] = r[1];
        }
        lengthy lcm = 1;
        for(lengthy de : d) {
            lcm = (lcm*de)/gcd(lcm,de);
        }
        for(lengthy[] r : lst) {
            lengthy f = lcm/r[1];
            consequence.append("("+String.format("%d,%d",r[0]*f,r[1]*f)+")");

        }
        return consequence.toString();

    }
    public static lengthy gcd(lengthy a, lengthy b) {
        if(b==0) return a;
        return gcd(b,apercentb);
    }

}

Take a look at instances to validate our answer

import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;

public class FractsTest {
	@Take a look at
  public void test_fractions() throws Exception {
    lengthy[][] lst;
    lst = new lengthy[][] { {1, 2}, {1, 3}, {10, 40} };
    assertEquals("(6,12)(4,12)(3,12)", Fracts.convertFrac(lst));  
  }
}

Further take a look at instances

import static org.junit.Assert.*;
import org.junit.Take a look at;

public class FractsTest {
    personal static String Array2D2String(lengthy[][] 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 += "]";
    }
    personal static void testing(lengthy[][] lst, String anticipated) {
        System.out.println("Testing " + Array2D2String(lst));
        String precise = Fracts.convertFrac(lst);   
        System.out.println("Precise " + precise);
        System.out.println("Count on " + anticipated);
        System.out.println("-");
        assertEquals(anticipated, precise);
    }
    
    @Take a look at
    public void take a look at() {
        lengthy[][] lst = new lengthy[][] { {1, 2}, {1, 3}, {1, 4} };
        testing(lst, "(6,12)(4,12)(3,12)");
        lst = new lengthy[][] { {69, 130}, {87, 1310}, {30, 40} };
        testing(lst, "(18078,34060)(2262,34060)(25545,34060)");       
        lst = new lengthy[][] {  }; 
        testing(lst, "");
        
        lst = new lengthy[][] { {77, 130}, {84, 131}, {3, 4} };
        testing(lst, "(20174,34060)(21840,34060)(25545,34060)");        
        lst = new lengthy[][] { {6, 13}, {187, 1310}, {31, 41} };
        testing(lst, "(322260,698230)(99671,698230)(527930,698230)");       
        lst = new lengthy[][] { {8, 15}, {7, 111}, {4, 25} };
        testing(lst, "(1480,2775)(175,2775)(444,2775)");
        
        lst = new lengthy[][] { {1, 2}, {1, 3}, {1, 4} };
        testing(lst, "(6,12)(4,12)(3,12)");       
        lst = new lengthy[][] { {77, 130}, {840, 1310}, {3, 4} };
        testing(lst, "(20174,34060)(21840,34060)(25545,34060)");        
        lst = new lengthy[][] { {1, 100}, {30, 10000}, {1, 2500}, {1, 20000} };
        testing(lst, "(200,20000)(60,20000)(8,20000)(1,20000)");
        
        lst = new lengthy[][] { {1, 1}, {3, 1}, {4, 1}, {5, 1} };
        testing(lst, "(1,1)(3,1)(4,1)(5,1)");        
        lst = new lengthy[][] { {3, 1} };
        testing(lst, "(3,1)");        
        lst = new lengthy[][] { {77, 130}, {84, 131}, {30, 40} };
        testing(lst, "(20174,34060)(21840,34060)(25545,34060)");       
        lst = new lengthy[][] { {1, 100}, {3, 1000}, {1, 2500}, {1, 20000} };
        testing(lst, "(200,20000)(60,20000)(8,20000)(1,20000)");
    }
    personal static int randInt(int min, int max) {
        return (int)(min + Math.random() * ((max - min) + 1));
    }
    //
    personal static lengthy gcd(lengthy a, lengthy b) {
        return b == 0 ? a : gcd(b, a % b);
    }
    personal static lengthy lcm(lengthy a, lengthy b) {
        return a * b / gcd(a, b);
    }
    personal static String convertFracHD(lengthy[][] lst) {
        lengthy lcmall = 1;
        lengthy[][] newlst = new lengthy[lst.length][2];
        for  (int i = 0; i < lst.size; i++) {
            lengthy 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 (lengthy[] merchandise : newlst) {
            lcmall = lcm(lcmall, merchandise[1]);
        }
        String consequence = "";
        for (lengthy[] merchandise : newlst) {
            consequence += "(" + (merchandise[0] * lcmall / merchandise[1]) + "," + lcmall + ")";
        }
        return consequence;
    }
    //
    personal static lengthy[][] doEx(int n) {
        lengthy[][] out = new lengthy[n][2];
        int j = 0;
        whereas (j < n) {
            out[j][0] = randInt(10, 80);
            out[j][1] = randInt(2, 5) * out[j][0];
            j += 1;
        }
        return out;
    }
    @Take a look at
    public void test1() {
        System.out.println("Random Exams ****");
        for (int i = 0; i < 100; i++) {
            lengthy[][] v = doEx(randInt(5, 10));
            String exp = convertFracHD(v);
            testing(v, exp);
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles