求最大公约数和最小公倍数算法

#
# def hcf(x,y):
#
#     # 求两个值的最大公约数
#
#     # 获取最小值
#     if x>y:
#         smaller=y
#     else:
#         smaller=x
#     for i in range(1,smaller+1):
#         if (x%i==0)and(y%i)==0:
#             hcf=i
#             print(i)
#     return hcf
#
# num1=int(input('请输入第一个数字'))
# num2=int(input('请输入第二个数字'))
# ret=hcf(num1,num2)
# print(ret)


def lcm(x,y):
    '''
    求最小公倍数
    :param x:
    :param y:
    :return:
    '''
    if x > y:
        greater=x
    else:
        greater=y
    while 1:
        if greater%x==0 and greater%y==0:
            break
        greater += 1

    return greater
num1=int(input('请输入第一个数字'))
num2=int(input('请输入第二个数字'))
ret=lcm(num1,num2)
print(ret)

 

posted @ 2018-11-25 20:01  团子emma  阅读(100)  评论(0)    收藏  举报