ko ko
ko ko

Reputation: 97

How to simplify fractions in my class?

This is my code for class Fraction:

class Fraction:
    """Class for performing fraction arithmetic.
    Each Fraction has two attributes: a numerator, n and a deconominator, d.
    Both must be integer and the deonominator cannot be zero.
    """
    def __init__(self,n,d):
        """Performs error checking and standardises to ensure denominator is positive"""
        if type(n)!=int or type(d)!=int:
            raise TypeError("n and d must be integers")
        if d==0:
            raise ValueError("d must be positive")
        elif d<0:
            self.n = -n
            self.d = -d
        else:
            self.n = n
            self.d = d

    def __str__(self):
        """Gives string representation of Fraction (so we can use print)"""
        return(str(self.n) + "/" + str(self.d))

    def __add__(self, otherFrac):
        """Produces new Fraction for the sum of two Fractions"""
        newN = self.n*otherFrac.d + self.d*otherFrac.n
        newD = self.d*otherFrac.d
        newFrac = Fraction(newN, newD)
        return(newFrac)

    def __sub__(self, otherFrac):
        """Produces new Fraction for the difference between two Fractions"""
        newN = self.n*otherFrac.d - self.d*otherFrac.n
        newD = self.d*otherFrac.d
        newFrac = Fraction(newN, newD)
        return(newFrac)

    def __mul__(self, otherFrac):
        """Produces new Fraction for the product of two Fractions"""
        newN = self.n*otherFrac.n
        newD = self.d*otherFrac.d
        newFrac = Fraction(newN, newD)
        return(newFrac)

    def __truediv__(self, otherFrac):
        """Produces new Fraction for the quotient of two Fractions"""
        newN = self.n*otherFrac.d
        newD = self.d*otherFrac.n
        newFrac = Fraction(newN, newD)
        return(newFrac)

    def __eq__(self,otherFrac):
        return(self.n * otherFrac.d) == (self.d * otherFrac.n)

To make the class more useful, how can I simplify the fraction?

For example: I want to change 30/15 into 5/3? It looks like:
(30/2)/(18/2) ---> 15/9 -----> (15/3)/(9/3) -----> 5/3

And I not use import fraction.

Upvotes: 1

Views: 2955

Answers (1)

Ben
Ben

Reputation: 6358

You want to find the greatest common divisor of the numerator and the denominator and divide both by that. The gcd function is in Python's standard library, but you might want to implement it yourself. One famous (and easy to implement) algorithm to find it is called Euclid's algorithm.

You can implement Euclid's algorithm by subtracting your two numbers to get a third number (the difference), then discarding the biggest number of the three and repeating this subtraction/discard process until one of your numbers is zero.

By the way, 30/15 reduced is 2/1.

To take your example (30/15)

30 - 15 = 15

Now you have 3 numbers (30, 15, 15). Discard the largest and repeat.

15 - 15 = 0

Now you have 3 smaller numbers (15, 15, 0).

15 - 0 = 15

Because that didn't change the set of numbers, you can conclude that 15 is your greatest common divisor. (and if you divide both 30 and 15 by 15, you get 2 and one, which is your reduced fraction's numerator and denominator.

Upvotes: 3

Related Questions