欧几里得方法计算最大公约数Python版本

def biggest_divisor(a, b):
    """ 欧几里得法获得最大公约数,递归要有停止条件"""
    if b == 0:
        return a
    else:
        return biggest_divisor(b, a % b)


def biggest_divisor_v2(a, b):
    while b>0:
        res = a % b
        a = b
        b = res
    return a


if __name__ == '__main__':
    print(biggest_divisor(14, 16))
    print(biggest_divisor_v2(14, 16))

 

posted @ 2021-04-21 22:32  今夜无风  阅读(152)  评论(0编辑  收藏  举报