Hướng dẫn python simplify fraction

still learning Python so any help is welcome. I have written a funtion to get a fraction, but more often than not the fraction can be simplified. My code does not do this yet. Any ideas how to simplify the fraction without using any import functions? code:

def add_frac(n1, d1, n2, d2):
    frac = (((n1 * d1) + (n2 * d2)), (d1 * d2))
    return frac

example: add_frac(1, 2, 1, 4) gives (6, 8) in stead of (3, 4)

Any help is welcome!

Hướng dẫn python simplify fraction

Tom

7,5712 gold badges14 silver badges30 bronze badges

asked Nov 20, 2020 at 14:38

Hướng dẫn python simplify fraction

A pretty naive approach to get you started, you can add some checks to make it more efficient.

def simplify_frac(n, d):
    i = 2
    while i < min(n, d) + 1:
        if n % i == 0 and d % i == 0:
            n = n // i
            d = d // i
        else:
            i += 1
    return n, d

# Some examples 
In [105]: simplify_frac(2, 4)
Out[105]: (1, 2)

In [106]: simplify_frac(16, 36)
Out[106]: (4, 9)

In [107]: simplify_frac(7, 3)
Out[107]: (7, 3)

In [108]: simplify_frac(10, 1)
Out[108]: (10, 1)

In [109]: simplify_frac(1, 10)
Out[109]: (1, 10)

In [110]: simplify_frac(102, 10)
Out[110]: (51, 5)

In [111]: simplify_frac(110, 10)
Out[111]: (11, 1)

We use a modulo operator % to check the remainder from integer division by i, if both n and d have a remainder of 0 we know i divides them.

answered Nov 20, 2020 at 14:51

Hướng dẫn python simplify fraction

In addition to above answers, if you cannot import any other libraries, here's is the implementation of fractions.gcd for python 2.7 (copied from https://stackoverflow.com/a/11175154/10155740)

>>> print inspect.getsource(gcd)
def gcd(a, b):
    """Calculate the Greatest Common Divisor of a and b.

    Unless b==0, the result will have the same sign as b (so that when
    b is divided by it, the result comes out positive).
    """
    while b:
        a, b = b, a%b
    return a

so if you'd incorporate this in your code, you should get something like:

def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

def add_frac(n1, d1, n2, d2):
    frac = (((n1 * d1) + (n2 * d2)), (d1 * d2))
    return tuple(i//gcd(*frac) for i in frac)

print(add_frac(1,2,1,4))
# OUTPUT: (3, 4)

answered Nov 20, 2020 at 14:56

Hướng dẫn python simplify fraction

QuadUQuadU

3061 silver badge3 bronze badges

Here is a solution with a recursive implementation of gcd, using Euclid Algorithm; it also works on negative numbers :

def mygcd(a, b) :
    return gcd_rec(abs(a), abs(b))

def gcd_rec(a,b) :
    if a*b == 0 : return a+b
    if a <= b : return gcd_rec(a, b % a)
    return gcd_rec(a % b, b)

def add_frac(n1, d1, n2, d2):
    n = n1*d2 + n2*d1
    d = d1*d2
    g = mygcd(n, d)
    return n//g, d//g

answered Nov 20, 2020 at 14:52

Hướng dẫn python simplify fraction

3

You need to divide it by GCD.

import math
def add_frac(n1, d1, n2, d2):
    nume = (n1 * d1) + (n2 * d2)
    deno = d1 * d2
    gcd = math.gcd(nume,deno)
    nume /= gcd
    deno /= gcd
    return (nume,deno)

>>> add_frac(1,2,1,4)
>>> (3.0, 4.0)

answered Nov 20, 2020 at 14:43

Hướng dẫn python simplify fraction

EquinoxEquinox

5,9373 gold badges20 silver badges30 bronze badges

The naive form of adding two fractions returns a correct answer, just not the most reduced correct answer. For that, you need to divide the numerator and denominator of the result by the greatest common denominator (GCD) of the original denominators. In this case, the GCD of 2 and 4 is 2, so dividing 6 and 8 by 2 gives the desired answer (3,4)

math.gcd can be used:

from math import gcd


def add_frac(n1, d1, n2, d2):
    x = gcd(d1, d2)
    frac = (((n1 * d2) + (n2 * d1)) / x, (d1 * d2) / x)
    return frac

though it sounds like you are expected to define your own implementation of gcd.

Hướng dẫn python simplify fraction

answered Nov 20, 2020 at 14:42

chepnerchepner

458k66 gold badges482 silver badges623 bronze badges

3

Not the answer you're looking for? Browse other questions tagged python function fractions simplify or ask your own question.