📜  python 简化分数 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:41.784000             🧑  作者: Mango

代码示例2
#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)