def gcd(a,b):
if a == 0:
return b
if b == 0:
return a
if a == b:
return a
if a > b:
return gcd(a-b, b)
return gcd(a, b-a)
def simplifyFraction(a, b):
divisor = gcd(a, b)
return (a / divisor, b / divisor)
import fractions
print(fractions.Fraction(1.5))
print(fractions.Fraction(5))
print(fractions.Fraction(1,3))
#From scratch
#Euclid's algorithm https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm
def gcd(a: int, b: int):
fraction = (a, b)
while fraction[0] != fraction[1]:
maximum = max(fraction)
minimum = max(fraction)
fraction = (maximum - minimum, minimum)
return fraction[0]
def simplify(a: int, b: int):
divisor = gcd(a, b)
return (a / divisor, b / divisor)