Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Fractions Addition by tokiojapan55
def gdc(a, b):
r = a % b
while r != 0:
a, b = b, r
r = a % b
return b
def add_fractions(fracts):
#replace this for solution
numerator,denominator = fracts[0]
for n,d in fracts[1:]:
numerator = numerator*d + n*denominator
denominator *= d
factor = gdc(numerator, denominator)
if factor > 1:
numerator //= factor
denominator //= factor
integer = numerator // denominator
surplus = numerator % denominator
if integer != 0 and surplus == 0:
result = integer
else:
result = '' if integer==0 else '{} and '.format(integer)
result += '{}/{}'.format(surplus, denominator)
return result
June 2, 2020
Comments: