Advertisement
calcpage

LACS07a_BigRational.java

Jun 12th, 2012
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.97 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac BigRational.java
  3.  *  Execution:    java BigRational
  4.  *
  5.  *  ADT for nonnegative Rational numbers. Uses Java's BigInteger
  6.  *  library for arbitrary precision. Does not support negative
  7.  *  integers or zero.
  8.  *
  9.  *  Invariant: all Rational objects are in reduced form (except
  10.  *  possibly while modifying).
  11.  *
  12.  *************************************************************************/
  13.  
  14. import java.math.BigInteger;
  15.  
  16. public class BigRational {
  17.     private BigInteger num;   // the numerator
  18.     private BigInteger den;   // the denominator
  19.  
  20.     // create and initialize a new Rational object
  21.     public BigRational(int numerator, int denominator) {
  22.         // BigInteger constructor takes a string, not an int
  23.         num = new BigInteger(numerator + "");
  24.         den = new BigInteger(denominator + "");
  25.         BigInteger g = num.gcd(den);
  26.         num = num.divide(g);
  27.         den = den.divide(g);
  28.     }
  29.  
  30.     // create and initialize a new Rational object
  31.     public BigRational(BigInteger numerator, BigInteger denominator) {
  32.         BigInteger g = numerator.gcd(denominator);
  33.         num = numerator.divide(g);
  34.         den = denominator.divide(g);
  35.     }
  36.  
  37.     // return string representation of (this)
  38.     public String toString() {
  39.         if (den.equals(BigInteger.ONE)) return num + "";
  40.         else                            return num + "/" + den;
  41.     }
  42.  
  43.     // return a * b
  44.     public BigRational times(BigRational b) {
  45.         BigRational a = this;
  46.         BigInteger numerator   = a.num.multiply(b.num);
  47.         BigInteger denominator = a.den.multiply(b.den);
  48.         return new BigRational(numerator, denominator);
  49.     }
  50.  
  51.  
  52.     // return a + b
  53.     public BigRational plus(BigRational b) {
  54.         BigRational a = this;
  55.         BigInteger numerator   = a.num.multiply(b.den).add(a.den.multiply(b.num));
  56.         BigInteger denominator = a.den.multiply(b.den);
  57.         return new BigRational(numerator, denominator);
  58.     }
  59.  
  60.     // return 1 / a
  61.     public BigRational reciprocal() { return new BigRational(den, num);  }
  62.  
  63.     // return a / b
  64.     public BigRational divides(BigRational b) {
  65.         BigRational a = this;
  66.         return a.times(b.reciprocal());
  67.     }
  68.  
  69.  
  70.    /*************************************************************************
  71.     *  Computes rational approximation to e using Taylor series
  72.     *
  73.     *      e = 1 + 1/1 + 1/2! + 1/3! + ... + 1/N!
  74.     *
  75.     *************************************************************************/
  76.  
  77.     public static void main(String[] args) {
  78.         int N = Integer.parseInt(args[0]);
  79.         BigRational r         = new BigRational(1, 1);
  80.         BigRational factorial = new BigRational(1, 1);
  81.         for (int i = 1; i <= N; i++) {
  82.             factorial = factorial.times(new BigRational(i, 1));
  83.             r = r.plus(factorial.reciprocal());
  84.             System.out.println(r);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement