Reduce Fractions Function Python
Join the DZone community and get the full member experience.Join For Free
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" greatest=gcd(n,d) n/=greatest d/=greatest return n, d
Opinions expressed by DZone contributors are their own.