tterrag1098

Untitled

Oct 4th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 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 int scaleNumerator(int denom) {
  90. if (num == 0) return 0;
  91. if (denom % this.denom != 0) {
  92. throw new IllegalArgumentException("Given denominator is not a multiple of the current denominator");
  93. }
  94. return num * (denom / this.denom);
  95. }
  96.  
  97. @Override
  98. public double doubleValue() {
  99. return (double) num / denom;
  100. }
  101.  
  102. @Override
  103. public float floatValue() {
  104. return (float) num / denom;
  105. }
  106.  
  107. @Override
  108. public int intValue() {
  109. return (int) doubleValue();
  110. }
  111.  
  112. @Override
  113. public long longValue() {
  114. return (long) doubleValue();
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. return num + "/" + denom;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment