[Python] 找兩個正整數的最大公因數及最小公倍數

公因數:

x = int(input("please input a number:"))
y = int(input("please input a number:"))
if x > y :
    c = x
else:
    c = y
for k in range(2, c):
    if x % k == 0 and y % k == 0:
        print(k)

結果:

please input a number:10
please input a number:20
2
5
10

最大公因數:

x = int(input("please input a number:"))
y = int(input("please input a number:"))
if x > y :
    c = x
else:
    c = y
for k in range(c, 2, -1):
    if x % k == 0 and y % k == 0:
        print(k)
        break

結果:

please input a number:10
please input a number:20
10

最小公倍數:

x = int(input("please input a number:"))
y = int(input("please input a number:"))
if x > y :
    c = x
else:
    c = y
m = (x + 1) * (y + 1)
for k in range(c, m):
    if k % x == 0 and k % y == 0:
        print(k)
        break

結果:

please input a number:10
please input a number:20
20