tterrag1098

Untitled

Oct 4th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1.     private static int lcm(Fraction a, Fraction b) {
  2.         return lcm(a.denom, b.denom);
  3.     }
  4.    
  5.     private static int lcm(int a, int b) {
  6.         if (a == 0 && b == 0) {
  7.             return 0;
  8.         }
  9.         int gcd = euclidGcd(a, b);
  10.         return (int) (Math.abs((long) a * (long) b) / gcd);
  11.     }
  12.    
  13.     private static int euclidGcd(int a, int b) {
  14.         a = Math.abs(a);
  15.         b = Math.abs(b);
  16.         // GCD of equal numbers is also equal to the numbers
  17.         if (a == b) {
  18.             return a;
  19.         }
  20.         // Algorithm depends on a > b
  21.         if (a < b) {
  22.             int temp = a;
  23.             a = b;
  24.             b = temp;
  25.         }
  26.         // Apply Euclid's algorithm
  27.         while (b != 0) {
  28.             int temp = b;
  29.             b = a % b;
  30.             a = temp;
  31.         }
  32.         return a;
  33.     }
  34.    
  35.     private static class Fraction extends Number {
  36.        
  37.         public final int num, denom;
  38.        
  39.         public Fraction() {
  40.             this(0);
  41.         }
  42.        
  43.         public Fraction(int num) {
  44.             this(num, 1);
  45.         }
  46.        
  47.         public Fraction(int num, int denom) {
  48.             if (denom == 0) {
  49.                 throw new IllegalStateException("Invalid fraction.");
  50.             }
  51.             // Move negative sign to numerator
  52.             if (denom < 0) {
  53.                 num = -num;
  54.                 denom = -denom;
  55.             }
  56.             // Always simplify fractions. Since fractions are immutable, this guarantees simplification of all fractions.
  57.             int gcd = euclidGcd(num, denom);
  58.             if (gcd > 1) {
  59.                 num /= gcd;
  60.                 denom /= gcd;
  61.             }
  62.             this.num = num;
  63.             this.denom = denom;
  64.         }
  65.        
  66.         public Fraction inverse() {
  67.             if (num == 0) {
  68.                 return this;
  69.             }
  70.             return new Fraction(denom, num);
  71.         }
  72.        
  73.         public Fraction add(Fraction other) {
  74.             int lcm = lcm(denom, other.denom);
  75.             int newNum = scaleNumerator(lcm) + other.scaleNumerator(lcm);
  76.             return new Fraction(newNum, lcm);
  77.         }
  78.        
  79.         public Fraction sub(Fraction other) {
  80.             int lcm = lcm(denom, other.denom);
  81.             int newNum = scaleNumerator(lcm) - other.scaleNumerator(lcm);
  82.             return new Fraction(newNum, lcm);
  83.         }
  84.        
  85.         public Fraction mul(Fraction other) {
  86.             return new Fraction(num * other.num, denom * other.denom);
  87.         }
  88.        
  89.         public Fraction div(Fraction other) {
  90.             return mul(other.inverse());
  91.         }
  92.        
  93.         public int scaleNumerator(int denom) {
  94.             if (num == 0) return 0;
  95.             if (denom % this.denom != 0) {
  96.                 throw new IllegalArgumentException("Given denominator is not a multiple of the current denominator");
  97.             }
  98.             return num * (denom / this.denom);
  99.         }
  100.  
  101.         @Override
  102.         public double doubleValue() {
  103.             return (double) num / denom;
  104.         }
  105.  
  106.         @Override
  107.         public float floatValue() {
  108.             return (float) num / denom;
  109.         }
  110.  
  111.         @Override
  112.         public int intValue() {
  113.             return (int) doubleValue();
  114.         }
  115.  
  116.         @Override
  117.         public long longValue() {
  118.             return (long) doubleValue();
  119.         }
  120.        
  121.         @Override
  122.         public String toString() {
  123.             return num + "/" + denom;
  124.         }
  125.     }
Advertisement
Add Comment
Please, Sign In to add comment