YChalk

Common Den

Feb 21st, 2022 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.util.*;
  2. import java.math.*;
  3. public class Main
  4. {
  5.     public static void main(String[] args) {
  6.         long[][] lst;
  7.         lst = new long[][] { {76, 380}, {52, 260}, {48, 144}, {40, 200}, {48, 144}, {64, 192}, {64, 256}, {78, 390}, {50, 100} };
  8.         System.out.println(convertFrac(lst));
  9.     }
  10.    
  11.     public static String convertFrac(long[][] lst) {
  12.     // your code
  13.     BigInteger[] num = new BigInteger[lst.length];
  14.     BigInteger[] den = new BigInteger[lst.length];
  15.     BigInteger common = new BigInteger("1");
  16.     /*long[] num = new long[lst.length];
  17.     long[] den = new long[lst.length];
  18.     long common = 1;*/
  19.    
  20.  
  21.     for (int i = 0; i < lst.length; i++){
  22.         /*num[i] = lst[i][0];
  23.         den[i] = lst[i][1];
  24.         common *= lst[i][1];*/
  25.         num[i] = new BigInteger(String.valueOf(lst[i][0]));
  26.         den[i] = new BigInteger(String.valueOf(lst[i][1]));
  27.         common = common.multiply(new BigInteger(String.valueOf(lst[i][1])));
  28.         //System.out.println(num[i] + " " + den[i] + " " + common);
  29.     }
  30.    
  31.     //System.out.println(common);
  32.    
  33.     for (int i = 0; i < num.length; i++){
  34.         BigInteger factor = common.divide(den[i]);
  35.         num[i] = num[i].multiply(factor);
  36.         //System.out.println(num[i]);
  37.     }
  38.    
  39.    
  40.    
  41.     BigInteger[] copy = Arrays.copyOf(num, num.length);
  42.     Arrays.sort(copy);
  43.    
  44.     for (BigInteger b : copy){
  45.         System.out.println(b);
  46.     }
  47.    
  48.     /*for (long i = copy[0]; i > 0; i--) {
  49.         final long I = i;
  50.         if ((Arrays.stream(num).filter(l -> l%I == 0).count() == num.length) && common%i == 0){
  51.             for (int j = 0; j < num.length; j++){
  52.                 num[j] /= i;
  53.             }
  54.             common /= i;
  55.         }
  56.     }*/
  57.    
  58.     StringBuilder sb = new StringBuilder("");
  59.    
  60.     for (BigInteger l : num) {
  61.         sb.append("(");
  62.         sb.append(l);
  63.         sb.append(",");
  64.         sb.append(common);
  65.         sb.append(")");
  66.     }
  67.    
  68.     return sb.toString();
  69.   }
  70. }
Add Comment
Please, Sign In to add comment