Over a million developers have joined DZone.

Reduce Fractions Function Python

DZone's Guide to

Reduce Fractions Function Python

Free Resource
A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python.

def reducefract(n, d):
    '''Reduces fractions. n is the numerator and d the denominator.'''
    def gcd(n, d):
        while d != 0:
            t = d
            d = n%d
            n = t
        return n
    assert d!=0, "integer division by zero"
    assert isinstance(d, int), "must be int"
    assert isinstance(n, int), "must be int"
    return n, d

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}