Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplifying fractions in Java

Tags:

My task is to develop a rational class. If 500 and 1000 are my inputs, then (½) must be my output. I have written a program on my own to find it.

Is there another best way to find the solution, or my program is already the best one?

public class Rational {      public static void main(String[] args){         int n1 = Integer.parseInt(args[0]);        int n2 = Integer.parseInt(args[1]);         int temp1 = n1;        int temp2 = n2;          while (n1 != n2){          if(n1 > n2)             n1 = n1 - n2;          else             n2 = n2 - n1;        }              int n3 = temp1 / n1 ;       int n4 = temp2 / n1 ;        System.out.print("\n Output :\n");        System.out.print(n3 + "/" + n4 + "\n\n" );       System.exit(0);     }   } 
like image 982
Pari Sairam Mohan Avatar asked Jul 08 '11 01:07

Pari Sairam Mohan


People also ask

What is fraction class in Java?

Description: This class provides storage for internal representation of, and methods to manipulate fractions. A fraction consists of two integers, one for numerator and one for denominator. An example fraction is 3/4. A valid fraction must not have zero in the denominator.


2 Answers

Interesting question. Here's some executable code that does it with minimal code:

/** @return the greatest common denominator */ public static long gcd(long a, long b) {     return b == 0 ? a : gcd(b, a % b); }  public static String asFraction(long a, long b) {     long gcd = gcd(a, b);     return (a / gcd) + "/" + (b / gcd); }  // Some tests public static void main(String[] args) {     System.out.println(asFraction(500, 1000)); //  "1/2"     System.out.println(asFraction(17, 3));     //  "17/3"     System.out.println(asFraction(462, 1071)); //  "22/51" } 

Bonus methods:

/** @return the lowest common multiple */ public static long lcm(long a, long b) {     return a * b / gcd(a, b); }  /** @return the greatest common denominator */ public static long gcd(List<? extends Number> numbers) {     return numbers.stream().map(Number::longValue).reduce((a, b) -> gcd(a, b)).orElseThrow(NoSuchElementException::new); }  /** @return the lowest common multiple */ public static long lcm(List<? extends Number> numbers) {     return numbers.stream().map(Number::longValue).reduce((a, b) -> lcm(a, b)).orElseThrow(NoSuchElementException::new); } 
like image 190
Bohemian Avatar answered Sep 20 '22 06:09

Bohemian


You need the GCD. Either use BigInteger like Nathan mentioned or if you can't, use your own.

public int GCD(int a, int b){    if (b==0) return a;    return GCD(b,a%b); } 

Then you can divide each number by the GCD, like you have done above.

This will give you an improper fraction. If you need a mixed fraction then you can get the new numbers. Example if you had 1500 and 500 for inputs you would end up with 3/2 as your answer. Maybe you want 1 1/2. So you just divide 3/2 and get 1 and then get the remainder of 3/2 which is also 1. The denominator will stay the same.

whole = x/y; numerator x%y; denominator = y; 

In case you don't believe me that this works, you can check out http://en.wikipedia.org/wiki/Euclidean_algorithm

I just happen to like the recursive function because it's clean and simple.

Your algorithm is close, but not exactly correct. Also, you should probably create a new function if you want to find the gcd. Just makes it a little cleaner and easier to read. You can also test that function as well.

like image 32
Matt Avatar answered Sep 17 '22 06:09

Matt